Source:
public void testAutoFlowRefreshTokenRequestFailedWithOauthError() throws IOException, InterruptedException { final FileMockContext mockContext = new FileMockContext(getContext()); final ITokenCacheStore mockCache = getCacheForRefreshToken(TEST_IDTOKEN_USERID, TEST_IDTOKEN_UPN); final AuthenticationContext context = getAuthenticationContext(mockContext, VALID_AUTHORITY, false, mockCache); final AggregatedTelemetryTestClass telemetry = new AggregatedTelemetryTestClass(); Telemetry.getInstance().registerDispatcher(telemetry, true); final CountDownLatch signal = new CountDownLatch(1); final MockActivity testActivity = new MockActivity(signal); final CountDownLatch signalCallback = new CountDownLatch(1); MockAuthenticationCallback callback = new MockAuthenticationCallback(signalCallback); final String responseBody = getErrorResponseBody("interaction_required"); final HttpURLConnection mockedConnection = Mockito.mock(HttpURLConnection.class); HttpUrlConnectionFactory.setMockedHttpUrlConnection(mockedConnection); Util.prepareMockedUrlConnection(mockedConnection); Mockito.when(mockedConnection.getOutputStream()).thenReturn(Mockito.mock(OutputStream.class)); Mockito.when(mockedConnection.getInputStream()).thenThrow(new IOException()); Mockito.when(mockedConnection.getErrorStream()).thenReturn(Util.createInputStream(responseBody)); Mockito.when(mockedConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_BAD_REQUEST); context.acquireToken(testActivity.getTestActivity(), "resource", "clientId", "redirect", TEST_IDTOKEN_UPN, callback); final int requestWaitMs = 200000; signal.await(requestWaitMs, TimeUnit.MILLISECONDS); assertEquals("Activity was attempted to start.", AuthenticationConstants.UIRequest.BROWSER_FLOW, testActivity.mStartActivityRequestCode); context.onActivityResult(testActivity.mStartActivityRequestCode, AuthenticationConstants.UIResponse.BROWSER_CODE_COMPLETE, getResponseIntent(callback, "resource", "clientid", "redirect", TEST_IDTOKEN_UPN)); signalCallback.await(CONTEXT_REQUEST_TIME_OUT, TimeUnit.MILLISECONDS); assertTrue(telemetry.checkOauthError()); assertTrue(telemetry.checkNoPIIPresent(EventStrings.LOGIN_HINT, TEST_IDTOKEN_UPN)); }

Target:
public void testAutoFlowRefreshTokenRequestFailedWithOauthError() throws IOException, InterruptedException { final FileMockContext mockContext = new FileMockContext(getContext()); final ITokenCacheStore mockCache = getCacheForRefreshToken(TEST_IDTOKEN_USERID, TEST_IDTOKEN_UPN); final AuthenticationContext context = getAuthenticationContext(mockContext, VALID_AUTHORITY, false, mockCache); final AggregatedTelemetryTestClass telemetry = new AggregatedTelemetryTestClass(); Telemetry.getInstance().registerDispatcher(telemetry, true); final CountDownLatch signal = new CountDownLatch(1); final MockActivity testActivity = new MockActivity(signal); final CountDownLatch signalCallback = new CountDownLatch(1); MockAuthenticationCallback callback = new MockAuthenticationCallback(signalCallback); final String responseBody = getErrorResponseBody("interaction_required"); final HttpURLConnection mockedConnection = Mockito.mock(HttpURLConnection.class); HttpUrlConnectionFactory.setMockedHttpUrlConnection(mockedConnection); Util.prepareMockedUrlConnection(mockedConnection); Mockito.when(mockedConnection.getOutputStream()).thenReturn(Mockito.mock(OutputStream.class)); Mockito.when(mockedConnection.getInputStream()).thenThrow(new IOException()); Mockito.when(mockedConnection.getErrorStream()).thenReturn(Util.createInputStream(responseBody)); Mockito.when(mockedConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_BAD_REQUEST); context.acquireToken(testActivity.getTestActivity(), "resource", "clientId", "redirect", TEST_IDTOKEN_UPN, callback); final int requestWaitMs = 200000; signal.await(requestWaitMs, TimeUnit.MILLISECONDS); assertEquals("Activity was attempted to start.", AuthenticationConstants.UIRequest.BROWSER_FLOW, testActivity.mStartActivityRequestCode); context.onActivityResult(testActivity.mStartActivityRequestCode, AuthenticationConstants.UIResponse.BROWSER_CODE_COMPLETE, getResponseIntent(callback, "resource", "clientid", "redirect", TEST_IDTOKEN_UPN)); signalCallback.await(CONTEXT_REQUEST_TIME_OUT, TimeUnit.MILLISECONDS); assertTrue(telemetry.checkOauthError()); assertTrue(telemetry.checkNoPIIPresent(EventStrings.LOGIN_HINT, TEST_IDTOKEN_UPN)); assertTrue(telemetry.checkAPIId("100")); assertTrue(telemetry.checkCacheEventCount("3")); assertFalse(telemetry.checkAPISucceeded()); }