NoSuchAlgorithmException when using PowerMockito

Ben

September 23, 2019

NoSuchAlgorithmException when using PowerMockito

So today I was making some changes to one of our services to add better support for rate limiting. I made all the changes and I was happy; but when I ran the tests to verify everything before I committed it. I was confronted with:

org.apache.http.ssl.SSLInitializationException: class configured for SSLContext: sun.security.ssl.SSLContextImpl$TLSContext not a SSLContext
at org.apache.http.ssl.SSLContexts.createDefault(SSLContexts.java:55)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.getSocketFactory(SSLConnectionSocketFactory.java:194)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.getDefaultRegistry(PoolingHttpClientConnectionManager.java:115)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:122)</init>
at com.intergral.cloud.remote.HTTPRequest.<clinit>(HTTPRequest.java:66)</clinit>
... 40 more
Caused by: java.security.NoSuchAlgorithmException: class configured for SSLContext: sun.security.ssl.SSLContextImpl$TLSContext not a SSLContext
at java.base/sun.security.jca.GetInstance.checkSuperClass(GetInstance.java:260)
at java.base/sun.security.jca.GetInstance.getInstance(GetInstance.java:237)
at java.base/sun.security.jca.GetInstance.getInstance(GetInstance.java:164)
at javax.net.ssl.SSLContext.getInstance(SSLContext.java:168)
at org.apache.http.ssl.SSLContexts.createDefault(SSLContexts.java:51)
... 44 more

Confused

I had not changed the test that was triggering the error, also the error is deep within Java and Apache HttpClient. It turns out the test in question is using PowerMockito to mock some legacy static functions for testing. This seems to have the side effect of affecting the class loader used to load the SSLContextSpi class used to verify the SSLContext type in the SSLContextFactory.

Solution

A quick google found that the solution is to exclude the javax.net.ssl.* package from the PowerMock system. This can be done by adding the @PowerMockIgnore annotation to the test class. I added the following to the test and it resolved the problem.

@PowerMockIgnore({ "javax.net.ssl.*" })

Ben

Ben

Experienced developer in various languages, currently a product owner of nerd.vision leading the back end architecture.