2

I have a json response something like this:

"someArray": [
    {
       "someProperty":"someValue",
       // other properties that are not relevant for me
    },
    {
       "someProperty":"someOtherValue",
       // other properties that are not relevant for me
    }
]

I want to check, if the someArray array has an object with property named "someProperty" with the value "someValue", but don't fail the test if it has another object with the same property but not the same value.

Is it possible? Until this I was using static index because I had only one element in that array.

Hienz
  • 688
  • 7
  • 21
  • Couldn't understand the question clearly. So the value of the property is irrelevant?Do you just want to test for the presence of property in the object? – Master Chief Apr 09 '19 at 08:29
  • You can extract List with all "someProperty" values and check if it contains "someValue". – bhusak Apr 09 '19 at 08:34
  • @bhusak and how exactly can I do that? Do I need to create a pojo for the objects with someProperty? cuz its a very big object and i dont really want to map it to a pojo. – Hienz Apr 09 '19 at 08:58

2 Answers2

5

Here's a solution using JsonPath:

List<String> values = RestAssured.when().get("/api")
    .then().extract().jsonPath()
    .getList("someArray.someProperty");

Assert.assertTrue(values.contains("someValue"));

Will work for following response JSON:

{  
   "someArray":[  
      {  
         "someProperty":"someValue"
      },
      {  
         "someProperty":"someOtherValue"
      }
   ]
}
bhusak
  • 1,320
  • 1
  • 9
  • 19
  • Wow, I was so close. I was messed up with getList's generic parameter, but it finally works. Thank you! – Hienz Apr 09 '19 at 10:20
1

Assuming you're using Java 8 or above, you should use Arrays.stream(someArray) and then use the filter method to select elements you desire.

I haven't used REST-assured but based on their documentation, it looks like you should be able to use something like this below

@Test public void
lotto_resource_returns_200_with_expected_id_and_winners() {

    when().
            get("/lotto/{id}", 5).
    then().
            statusCode(200).
            body("someArray", hasItems(hasEntry("someProperty", "someValue")));

}

This works if you can put some kind of deserialization logic to convert object to map before using hasEntry

Another solution is to use findAll body("someArray.findAll {o -> o.someProperty == 'someValue'}.size()", greaterThan(0))

  • Yea, that's a simple one, but someArray.someProperty won't work, since someArray is an array and not an object with properties. Basically what I need: Run through an array in json response, check if the objects inside have "someProperty" property and if so, then assert it's value. I cannot map the objects inside to a POJO, since they have serveral properties that are not relevant. – Hienz Apr 09 '19 at 09:46
  • I just looked up on rest-assured and found `hasEntry` which may be what you're looking for. https://github.com/rest-assured/rest-assured/blob/master/json-path/src/test/java/io/restassured/path/json/JsonPathTest.java. See my updated answer. – Deepak Khillare Apr 09 '19 at 09:52
  • Tried it, but i got: Expected: a collection containing map containing ["someProperty"->"someValue"] Actual: // it printed here my whole array – Hienz Apr 09 '19 at 09:58
  • I realized the hasEntry is specific to Map interface. You'd need to de-serialize the object to map before using the hasEntry. – Deepak Khillare Apr 09 '19 at 10:06