When a TrustManagerFactory is not a TrustManagerFactory (Java)

David Beveridge picture David Beveridge · Feb 1, 2013 · Viewed 8.6k times · Source

I am trying to add some additional JUnit test to an existing App-Server (TomCat) product. I have run into an issue with the (existing and fielded) custom TrustManager. This thing works fine in production, but during JUnit, gives exception.

The customized TrustManager merely loads a keystore from a path, and implicitly trusts our own public certs. For some reason, using this in JUnits causes an exception on the following line:

TrustManagerFactory tmFactory = TrustManagerFactory.getInstance("PKIX");    

Exception:

java.security.NoSuchAlgorithmException:  class configured for TrustManagerFactory: com.sun.net.ssl.internal.ssl.TrustManagerFactoryImpl$PKIXFactory not a TrustManagerFactory

This exception takes place regardless of what Provider/Algorithm combinations are used ("SunX509", .getDefaultAlgorithm(), et al.).

Any insight will be greatly appreciated.

Answer

GBa picture GBa · May 29, 2014

Well, looks like PowerMock messes up with SSL issues and thus, you run into loading a wrong factory. The solution for that is to use an annotation on the test class:

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

this is taken from https://groups.google.com/forum/#!topic/powermock/v4nreP2AnOQ