9

I have the following code which should test my RESTful API:

given().baseUri("http://...").get("/categories/all")
    .then()
    .body(
        "results",  not(empty())
    );

The API returns the following response:

{
    "error": {
        "type": "NotFoundException"
    }
}

And I want the test to fail for such response. But the test passes.

How can I modify the test to make it fail? It should only pass if the API returns an object which contains a not-empty array in the "results" key. It should fail when the "results" key does not exists, contains empty array or when it contains something that is not an array.

6
  • Why not return a List instead, and use the .isEmpty() check ? Commented Jan 20, 2015 at 7:43
  • Just a note: if you want to be RESTful, do not return error code in the body response, but use standard HTTP return codes (Not Found is 404) Commented Jan 20, 2015 at 7:43
  • @Stultuske I do not fully understand your comment. Should I return a list from the REST API? But the API does not work correctly, it does not return a list and I want to check it. Commented Jan 20, 2015 at 7:48
  • Much depends on how you implement it. If you don't want to change the code, you can still go Arrays.asList(myArrayName); and call isEmpty() on that Commented Jan 20, 2015 at 7:49
  • @ThanksForAllTheFish I know :) . I do return the 404 response code. The response contents only contain additional information. But in my test I want to check the "results" key anyway. Commented Jan 20, 2015 at 7:51

4 Answers 4

10

I came up with the following solution:

given().baseUri("http://...").get("/categories/all")
    .then()
    .body(
        "results", hasSize(greaterThan(0))
    );

It fails if "results" is an empty array or not an array. It passes if "results" is a not-empty array. It reports an error in a readable way, e.g.:

Expected: a collection with size a value greater than <0>
Actual: null
Sign up to request clarification or add additional context in comments.

Comments

6

I had a similar problem but in my case the endpoint direcly returns an array. My solution for this:

@Test
public void testNotEmpty() {
    uAssured.given()
            .when()
                .get("resources/totest")
            .then()
                .statusCode(200)
                .body("$.size()", greaterThan(0));
}

For the example above the following should work as well:

@Test
public void testNotEmpty() {
    uAssured.given()
            .when()
                .get("resources/totest")
            .then()
                .statusCode(200)
                .body("results.size()", greaterThan(0));
}

Comments

0

I was having the same problem and what I found was that the following instruction worked:

given().baseUri("http://...").get("/categories/all")
.then()
.assertThat().body(notNullValue());

I tried everything above and had no luck.

Comments

0

To check if the array is not empty you can use the not(emptyArray) method.

   given()
  .baseUri("http://...")
  .get("/categories/all")
  .then()
  .body("results", not(emptyArray()));

use emptyArray method from org.hamcrest.Matcher

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.