### Question:
OfflineCredentials { public Credential generateCredential() throws OAuthException { GoogleCredential credential = Strings.isNullOrEmpty(this.jsonKeyFilePath) ? generateCredentialFromClientSecrets() : generateCredentialFromKeyFile(); try { if (!oAuth2Helper.callRefreshToken(credential)) { throw new OAuthException( "Credential could not be refreshed. A newly generated refresh token or " + "secret key may be required."); } } catch (IOException e) { throw new OAuthException("Credential could not be refreshed.", e); } return credential; } private  OfflineCredentials(ForApiBuilder builder); HttpTransport getHttpTransport(); String getRefreshToken(); String getClientId(); String getClientSecret(); String getJsonKeyFilePath(); Credential generateCredential();  }### Answer:
@Test public void testGenerateCredential() throws Exception { HttpTransport httpTransport = new NetHttpTransport(); OfflineCredentials offlineCredentials = new OfflineCredentials.Builder(oAuth2Helper) .forApi(OfflineCredentials.Api.AD_MANAGER) .withClientSecrets("clientId", "clientSecret") .withRefreshToken("refreshToken") .withHttpTransport(httpTransport) .build(); when(oAuth2Helper.callRefreshToken(Mockito.<Credential>anyObject())).thenReturn(true); Credential credential = offlineCredentials.generateCredential(); assertEquals( "clientId", ((ClientParametersAuthentication) credential.getClientAuthentication()).getClientId()); assertEquals( "clientSecret", ((ClientParametersAuthentication) credential.getClientAuthentication()).getClientSecret()); assertEquals("refreshToken", credential.getRefreshToken()); assertSame(httpTransport, credential.getTransport()); }
@Test public void testGenerateCredential_IOException() throws Exception { OfflineCredentials offlineCredentials = new OfflineCredentials.Builder(oAuth2Helper) .forApi(OfflineCredentials.Api.AD_MANAGER) .withClientSecrets("clientId", "clientSecret") .withRefreshToken("refreshToken") .build(); when(oAuth2Helper.callRefreshToken(Mockito.<Credential>anyObject())) .thenThrow(new IOException()); thrown.expect(OAuthException.class); offlineCredentials.generateCredential(); }
@Test public void testGenerateCredential_cantRefresh() throws Exception { OfflineCredentials offlineCredentials = new OfflineCredentials.Builder(oAuth2Helper) .forApi(OfflineCredentials.Api.AD_MANAGER) .withClientSecrets("clientId", "clientSecret") .withRefreshToken("refreshToken") .build(); when(oAuth2Helper.callRefreshToken(Mockito.<Credential>anyObject())).thenReturn(false); thrown.expect(OAuthException.class); offlineCredentials.generateCredential(); }