Issue
The issue is that the list of ids
within the pageRequest
object can be in any order. Is there a way to specify that below? The test fails since the pageRequest
has a list of ids
in a different order than the one specified below. The "when" clause returns null in that case.
PageRequest pageRequest = PageRequest.builder()
.ids(List.of(serviceId2, serviceId1, serviceId3))
.build();
when(client.getServices(eq(pageRequest))).thenAnswer(a -> {.......
Solution
You can implement a custom ArgumentMatcher
to define the matching logic and then use argThat
with this ArgumentMatcher
during stubbing :
@Test
public void test(){
when(someMock.getSerivce(argThat(pageRequestContainId(List.of(1,4,5,6))))).thenReturn(blablabla);
}
private ArgumentMatcher<PageRequest> pageRequestContainId(List<Integer> ids) {
return pageReq -> pageReq.ids.containsAll(ids) && pageReq.ids.size() == ids.size();
}
Answered By - Ken Chan
Answer Checked By - Mary Flores (JavaFixing Volunteer)