Source:
public void testValidUserCredential() throws Exception { String username = "testuser"; String password = "P@ssw0rd"; String token1 = "token1"; String token2 = "token2"; TokenRequest request1 = new TokenRequest().addScopes("<LINK_1>"); TokenRequest request2 = new TokenRequest().addScopes("<LINK_0>"); OffsetDateTime expiresOn = OffsetDateTime.now(ZoneOffset.UTC).plusHours(1); IdentityClient identityClient = PowerMockito.mock(IdentityClient.class); when(identityClient.authenticateWithUsernamePassword(request1, username, password)).thenReturn(TestUtils.getMockMsalToken(token1, expiresOn)); when(identityClient.authenticateWithUserRefreshToken(any(), any())) .thenAnswer(invocation -> { TokenRequest argument = (TokenRequest) invocation.getArguments()[0]; if (argument.getScopes().size() == 1 && argument.getScopes().get(0).equals(request2.getScopes().get(0))) { return TestUtils.getMockMsalToken(token2, expiresOn); } else if (argument.getScopes().size() == 1 && argument.getScopes().get(0).equals(request1.getScopes().get(0))) { return Mono.error(new UnsupportedOperationException("nothing cached")); } else { throw new InvalidUseOfMatchersException(String.format("Argument %s does not match", (Object) argument)); } }); PowerMockito.whenNew(IdentityClient.class).withAnyArguments().thenReturn(identityClient); UsernamePasswordCredential credential = new UsernamePasswordCredentialBuilder().clientId(clientId).username(username).password(password).build(); AccessToken token = credential.getToken(request1).block(); Assert.assertEquals(token1, token.getToken()); Assert.assertEquals(expiresOn.getSecond(), token.getExpiresOn().getSecond()); token = credential.getToken(request2).block(); Assert.assertEquals(token2, token.getToken()); Assert.assertEquals(expiresOn.getSecond(), token.getExpiresOn().getSecond()); }

Target:
public void testValidUserCredential() throws Exception { String username = "testuser"; String password = "P@ssw0rd"; String token1 = "token1"; String token2 = "token2"; TokenRequest request1 = new TokenRequest().addScopes("<LINK_1>"); TokenRequest request2 = new TokenRequest().addScopes("<LINK_0>"); OffsetDateTime expiresOn = OffsetDateTime.now(ZoneOffset.UTC).plusHours(1); IdentityClient identityClient = PowerMockito.mock(IdentityClient.class); when(identityClient.authenticateWithUsernamePassword(request1, username, password)).thenReturn(TestUtils.getMockMsalToken(token1, expiresOn)); when(identityClient.authenticateWithUserRefreshToken(any(), any())) .thenAnswer(invocation -> { TokenRequest argument = (TokenRequest) invocation.getArguments()[0]; if (argument.getScopes().size() == 1 && argument.getScopes().get(0).equals(request2.getScopes().get(0))) { return TestUtils.getMockMsalToken(token2, expiresOn); } else if (argument.getScopes().size() == 1 && argument.getScopes().get(0).equals(request1.getScopes().get(0))) { return Mono.error(new UnsupportedOperationException("nothing cached")); } else { throw new InvalidUseOfMatchersException(String.format("Argument %s does not match", (Object) argument)); } }); PowerMockito.whenNew(IdentityClient.class).withAnyArguments().thenReturn(identityClient); UsernamePasswordCredential credential = new UsernamePasswordCredentialBuilder().clientId(clientId).username(username).password(password).build(); StepVerifier.create(credential.getToken(request1)) .expectNextMatches(accessToken -> token1.equals(accessToken.getToken()) && expiresOn.getSecond() == accessToken.getExpiresOn().getSecond()) .verifyComplete(); StepVerifier.create(credential.getToken(request2)) .expectNextMatches(accessToken -> token2.equals(accessToken.getToken()) && expiresOn.getSecond() == accessToken.getExpiresOn().getSecond()) .verifyComplete(); }