unit testing - How to assert attribute of contents of a list using Assertj? -


such method.

list<user> somemethod() {     user user[] = new users[3];     user[0] = new user();     user[0].setimage(new image(constants.home));     user[1] = new user();     user[1].setimage(new image(constants.not_home));     user[2] = new user();     user[2].setimage(new image(constants.home));        return arrays.aslist(user); } 

how can test test above code. need verify of users have images on home.

i try re-build entire users list , compare it. but, not want that.

i tried this,

assertthat(optional.of(service.somemethod())).hasvaluesatisfying((a)->a.stream().allmatch((b)->b.getimage().getimageurl().equals(constants.home))); 

the assertion true in context, because hasvaluesatisfying using consumer<t> parameter. but, want use in in following way - assertj way

can use hamcrest in way solve this?

i use assertj extracting feature:

assertthat(users).extracting(user -> getimage().getimageurl())                  .contains(constants.home) 

i'm assuming here users list<user> not optional of list<user>.


Comments