### Question:
SauceLabsRestApi { public synchronized boolean isAuthenticated(String username, String apiKey) { LOGGER.entering(); final String key = username + ":" + apiKey; final String authKey = new String(Base64.encodeBase64(key.getBytes())); if (accountCache.containsKey(md5(authKey))) { final boolean authenticated = accountCache.get(md5(authKey)); LOGGER.exiting(authenticated); return authenticated; } SauceLabsHttpResponse response; try { final URL url = new URL(SauceConfigReader.getInstance().getSauceURL() + "/users/" + username); response = doSauceRequest(url, authKey, sauceTimeout, 0); if (response.getStatus() == HttpStatus.SC_OK) { addToAccountCache(md5(authKey), true); LOGGER.exiting(true); return true; } } catch (IOException e) { LOGGER.log(Level.SEVERE, "Unable to communicate with sauce labs api.", e); } addToAccountCache(md5(authKey), false); LOGGER.exiting(false); return false; } SauceLabsRestApi(); int getNumberOfTCRunning(); int getNumberOfTCRunningForSubAccount(String user); int getMaxConcurrency(); synchronized boolean isAuthenticated(String username, String apiKey); static final int MAX_CACHE; }### Answer:
@Test public void isAuthenticated() throws Exception { SauceLabsHttpResponse mockHttpResponse = mock(SauceLabsHttpResponse.class); doReturn(HttpStatus.SC_OK).when(mockHttpResponse, "getStatus"); SauceLabsRestApi apiMock = mock(SauceLabsRestApi.class); Whitebox.setInternalState(apiMock, "accountCache", new HashMap<String, Boolean>()); doReturn(mockHttpResponse).when(apiMock, "doSauceRequest", Mockito.any(), Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt()); when(apiMock.isAuthenticated(Mockito.anyString(), Mockito.anyString())).thenCallRealMethod(); assertTrue(apiMock.isAuthenticated("foo", "bar")); }