Skip to content

Instantly share code, notes, and snippets.

@ecgreb
Last active February 1, 2017 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ecgreb/d2c3e8552ae9b880a542ce60e123a15f to your computer and use it in GitHub Desktop.
Save ecgreb/d2c3e8552ae9b880a542ce60e123a15f to your computer and use it in GitHub Desktop.
public static void initMockSslContext() throws KeyStoreException, NoSuchAlgorithmException {
final TrustManagerFactory trustManagerFactory = PowerMockito.mock(TrustManagerFactory.class);
final TrustManager[] trustManagers = new TrustManager[1];
// Mock TrustManagerFactory.getInstance(String)
mockStatic(TrustManagerFactory.class);
when(TrustManagerFactory.getInstance(anyString())).thenReturn(trustManagerFactory);
// Mock TrustManagerFactory#getTrustManagers()
trustManagers[0] = PowerMockito.mock(X509TrustManager.class);
doNothing().when(trustManagerFactory).init(any(KeyStore.class));
when(trustManagerFactory.getTrustManagers()).thenReturn(trustManagers);
// Mock SSLContext.getInstance(String)
mockStatic(SSLContext.class);
when(SSLContext.getInstance(anyString())).thenReturn(PowerMockito.mock(SSLContext.class));
// Mock CertificateChainCleaner.get(X509TrustManager)
mockStatic(CertificateChainCleaner.class);
when(CertificateChainCleaner.get(any(X509TrustManager.class)))
.thenReturn(PowerMockito.mock(CertificateChainCleaner.class));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment