msgraph-sdk-java
msgraph-sdk-java copied to clipboard
Question: how to mock GraphServiceClient properly when using with PageIterator ?
I'm using GraphServiceClient along with PageIterator in my code to go over all response pages.
UserCollectionResponse userCollectionResponse = graphClient.users().get();
List<User> users = new LinkedList<>();
PageIterator<User, UserCollectionResponse> graphResponsePageIterator = new PageIterator.Builder<User, UserCollectionResponse>()
.client(client)
.collectionPage(userCollectionResponse)
.collectionPageFactory(UserCollectionResponse::createFromDiscriminatorValue)
.processPageItemCallback(user -> {
users.add(user);
return true;
}).build();
graphResponsePageIterator.iterate();
I need to test my code. How to mock GraphServiceClient methods/responses properly to make it satisfy PageIterator invocations?
minimal mocking which I found working is :
var client = Mockito.mock(GraphServiceClient.class, RETURNS_DEEP_STUBS);
var user = new User();
user.setId("43");
user.setDisplayName("43");
user.setMail("43");
when(client.users().get().getValue()).thenReturn(List.of(user));
when(client.users().get().getFieldDeserializers().containsKey("value")).thenReturn(true);
but it only works when a single test. When there are multiple @Test in a suit, they fail sporadically from time to time.
How to mock responses of GraphServiceClient to make it work with PageIterator?