Reputation: 175
I actually have two types of data:
a = ["1", "2", "3", "3", "5"]
b = ["7", "2"]
given()
.header("Content-Type", "application/json").
when()
.post(this.url).
then()
.statusCode(200)
.contentType("application/json")
.body(myPathToData, everyItem(haveOneOrMoreElementFrom(a)));
I would like to test, with Hamcrest, in my body response after my rest-assured request, if b (element received) contains one or more element from a (haveOneOrMoreElementFrom
in my example).
Is it also possible to make a function in my body response?
Solution:
I found a possible solution:
everyItem(hasItem(isIn(a)))
Upvotes: 6
Views: 4028
Reputation: 3267
I believe that the solution to check if b
contains at least one element from a
is
assertThat(b, hasItemInArray(isIn(a))); // for arrays
assertThat(b, hasItem(isIn(a))); // for collections
In case of Rest-assured, it would be
...
then()
.body(pathToData, hasItem(isIn(a)));
In Hamcrest 2 org.hamcrest.Matchers::isIn
methods are deprecated, so it is recommended to use is(in(a))
or just in(a)
instead.
Upvotes: 4