Reputation: 1035
I am unable to mock a class (using PowerMock's createMock method). This class is creating an object of ThreadSafeClientConnManager class in its constructor. I get exception at the line where I called createMock method passing my class name. Following is the exception stack trace. It is working fine if I run an integration test however. What could be the issue ?
[junit] Caused by: java.lang.IllegalStateException: Failure initializing default SSL context
[junit] at org.apache.http.conn.ssl.SSLSocketFactory.createDefaultSSLContext(SSLSocketFactory.java:211)
[junit] at org.apache.http.conn.ssl.SSLSocketFactory.<init>(SSLSocketFactory.java:333)
[junit] at org.apache.http.conn.ssl.SSLSocketFactory.getSocketFactory(SSLSocketFactory.java:165)
[junit] at org.apache.http.impl.conn.SchemeRegistryFactory.createDefault(SchemeRegistryFactory.java:45)
[junit] at org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager.<init>(ThreadSafeClientConnManager.java:98)
[junit] at com.amazon.marketplace.payment.tsf.TSFClient.<init>(TSFClient.java:109)
[junit] at com.amazon.marketplace.payment.tsf.TSFClient.<clinit>(TSFClient.java:95)
[junit] Caused by: java.security.NoSuchAlgorithmException: class configured for KeyManagerFactory: com.sun.net.ssl.internal.ssl.KeyManagerFactoryImpl$SunX509 not a KeyManagerFactory
[junit] at sun.security.jca.GetInstance.checkSuperClass(GetInstance.java:242)
[junit] at sun.security.jca.GetInstance.getInstance(GetInstance.java:221)
[junit] at sun.security.jca.GetInstance.getInstance(GetInstance.java:147)
[junit] at javax.net.ssl.KeyManagerFactory.getInstance(KeyManagerFactory.java:121)
[junit] at org.apache.http.conn.ssl.SSLSocketFactory.createSSLContext(SSLSocketFactory.java:184)
[junit] at org.apache.http.conn.ssl.SSLSocketFactory.createDefaultSSLContext(SSLSocketFactory.java:209)
Upvotes: 6
Views: 1552
Reputation: 847
Are you sure the class is creating the ThreadSafeClientConnManager in its constructor? Could it be doing it in a static initializer instead? If that's the case, you might get around that by adding the following two annotations before the "class" definition in your test class, e.g.:
@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassUnderTest.class)
public class YourTestClass {
Note the <clinit> in the parent initialization, indicating part of the class's static initialization process.
Upvotes: 2
Reputation: 635
Do you think it's a system class loading issue? try referring to this link it might give you some pointers . http://code.google.com/p/powermock/wiki/MockSystem.
Upvotes: 2