Search code examples
testingrest-assuredrest-assured-jsonpath

JSON path title doesn't match


I write a simple test using rest-assured, code below

@Test
public void exampleRestTest() {
    RestAssured.baseURI = "https://jsonplaceholder.typicode.com/";

    Response res = RestAssured.get("posts/1");

    res.print();
    res.then()
            .body("id", equalTo(1))
            .body("userId", equalTo(1))
            .body("title",contains("sunt aut facere repellat provident occaecati excepturi optio reprehenderit"));

JSON from res.print() is:

{
  "userId": 1,
  "id": 1,
  "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
  "body": "quia et suscipit suscipit recusandae consequuntur expedita et cum reprehenderit molestiae ut ut quas totam nostrum rerum est autem sunt rem eveniet architecto"
}

Test output:

java.lang.AssertionError: 1 expectation failed.
JSON path title doesn't match.
Expected: iterable containing ["sunt aut facere repellat provident occaecati excepturi optio reprehenderit"]
  Actual: sunt aut facere repellat provident occaecati excepturi optio reprehenderit

Why? I must escape from iterable containing?


Solution

  • Do you want to use 'contains' matcher only? 'contains' matcher checks that expected Iterable object is contained in the actual collection object.

    You should use 'containsString' matcher as follows:

    .body("title",containsString("sunt aut facere repellat provident occaecati excepturi optio reprehenderit"))