36

How can I make sure that my response, let's say it is in JSON, either contains or does not contain a specific field?

when()
    .get("/person/12345")
.then()
    .body("surname", isPresent()) // Doesn't work...
    .body("age", isNotPresent()); // ...But that's the idea.

I'm looking for a way to assert whether my JSON will contain or not the fields age and surname.

juliaaano
  • 1,307
  • 1
  • 13
  • 11

4 Answers4

80

You can use the Hamcrest matcher hasKey() (from org.hamcrest.Matchers class) on JSON strings as well.

when()
    .get("/person/12345")
.then()
    .body("$", hasKey("surname"))
    .body("$", not(hasKey("age")));
Amit K. Saha
  • 5,871
  • 2
  • 27
  • 35
Luciano van der Veekens
  • 6,307
  • 4
  • 26
  • 30
  • 10
    This is the correct answer, why not to mark it as one? – Konstantin Ivanov Mar 12 '18 at 12:33
  • 4
    for knowledge without clicking further links: hasKey() method comes from class org.hamcrest.Matchers – bucky Feb 28 '19 at 09:40
  • if it array and you want in place 1 example .body("$", not(hasKey(array[1]))); it will not work – Vladi Mar 13 '19 at 11:06
  • 1
    Wat if it's a nested json? I am guessing this "$" in the above expression would change to a descriptive path like "$.nameOfTheNode.AnotherNode" ? – Monnie_tester Feb 05 '21 at 17:45
  • I tried `body("$", not(hasKey("...")))` and it passes **also** for **existing** elements. I doubt it is correct. E.g. this passes: `.body("ultimate.answer", is("42")).body("$", not(hasKey("ultimate.answer")))` when the body contains `42` – Honza Zidek Jul 29 '21 at 15:52
0

You can use equals(null) like this:

.body("surname", equals(null))

If the field does not exists it will be null

J. N
  • 193
  • 1
  • 5
  • 5
    This also matches on a JSON field set to null. – Luciano van der Veekens Mar 27 '17 at 11:57
  • You are right but if it receives null it's basically the same – J. N Mar 27 '17 at 11:58
  • 1
    It's not the same, because if a field is not set, then a JavaScript client will see it as `undefined` and not as `null`. – ytg Aug 08 '19 at 13:04
  • If you are trying to extract data from XML, and check is it empty, that approach will not work. The right one is: body("$", not(hasKey("user.age"))) – Zhivko.Kostadinov Oct 09 '19 at 13:02
  • @Zhivko.Kostadinov: I tried your `body("$", not(hasKey("...")))` and it passes **also** for **existing** elements. I doubt it is correct. E.g. this passes: `.body("ultimate.answer", is("42")).body("$", not(hasKey("ultimate.answer")))` when the body contains `42` – Honza Zidek Jul 29 '21 at 15:46
0

Check whetever field is null. For example:

    Response resp = given().contentType("application/json").body(requestDto).when().post("/url");
    ResponseDto response = resp.then().statusCode(200).as(ResponseDto.class);
    Assertions.assertThat(response.getField()).isNotNull();
-1

Using: import static org.junit.Assert.assertThat;

Your could then: assertThat(response.then().body(containsString("thingThatShouldntBeHere")), is(false));

ForJaysSake
  • 50
  • 1
  • 4