2

I have an HTTP response body that looks that this when I make a GET request:

 [
  {
    "id": "1111",
    "type": "Sale",
    "name": "MyNameTest",
    "shortDescription": "Sale a"
  }
]

When I try to assert the results with "Rest Assured", the name value is always wrapped in square brackets [].

    final String returnedAttributeValue = response.jsonPath().getString("name");
    Assert.assertEquals(returnedAttributeValue, "MyNameTest");

So the test fails with Expected "MyNameTest", but was "[MyNameTest]"

Can any one tell me how to resolve this?

Dennis Kozevnikoff
  • 2,078
  • 3
  • 19
  • 29
R111
  • 151
  • 1
  • 6
  • 14

2 Answers2

3

You are accessing values within an array, so use name[n]

final String returnedAttributeValue = response.jsonPath().getString("name[0]");
Assert.assertEquals(returnedAttributeValue, "MyNameTest");
Wilfred Clement
  • 2,674
  • 2
  • 14
  • 29
0

Another possible solution for checking the content of an array, you could use org.hamcrest.Matchers.contains:

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.hamcrest.Matchers.contains;

response
  .andExpect(jsonPath("$name", contains("MyNameTest")));
Felipe
  • 7,013
  • 8
  • 44
  • 102