1

I am trying to mock some objects with Mockito, but my code doesn't work. My code contains one final method sslContext.init(..,..,..), and I don't know how to fix that.

This is my code that I want to mock :

public class MYClass {
private SSLSocketFactory sslSocketFactory;   
public MyClass() throws IOException{
    String trustStoreFilePath = System.getProperty("javax.net.ssl.trustStore");
    try {
            TrustManagerFactory trustManagerFactory = CertManagerFactory.loadTrustStore(trustStoreFilePath);
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
            this.sslSocketFactory = sslContext.getSocketFactory();
        } 
        catch (NoSuchAlgorithmException e) {} 
        catch (KeyManagementException e) {}
    }
}

and my code is :


@RunWith(PowerMockRunner.class)
@PrepareForTest( {CertManagerFactory.class, SSLContext.class} )
public class MyClassTest{

@Before
public void setUp() throws IOException, NoSuchAlgorithmException, KeyManagementException {

    PowerMockito.mockStatic(CertManagerFactory.class, SSLContext.class);        
    SSLContext sslContextMock = mock(SSLContext.class);
    SSLSocketFactory sSLSocketFactoryMock = mock(SSLSocketFactory.class);
    TrustManagerFactory trustManagerFactoryMock = mock(TrustManagerFactory.class);   PowerMockito.when(CertManagerFactory.loadTrustStore(anyString())).thenReturn(trustManagerFactoryMock);
    PowerMockito.when(SSLContext.getInstance(anyString())).thenReturn(sslContextMock);
    when(trustManagerFactoryMock.getTrustManagers()).thenReturn(any(TrustManager[].class));
    when(sslContextMock.getSocketFactory()).thenReturn(sSLSocketFactoryMock);
    MyClass myClass= new MyClass();

}

bakoyaro
  • 2,550
  • 3
  • 36
  • 63
houchker
  • 103
  • 1
  • 2
  • 11
  • Is your question about Mockito or PowerMockito ? – Michael Easter Oct 17 '14 at 02:37
  • I don't answer to this question, but to mine (Google takes me here...) I use mockserver. I needed to mock an SSLContext. mockserver provides already an SSLFactory that allows to make a mocked SSLContext. – mcoolive Sep 29 '16 at 09:16

1 Answers1

2

I think you need to mock the specific final method. Can you try to add this line after SSLContext sslContextMock = mock(SSLContext.class);

PowerMockito.doNothing().when(sslContextMock).init(any(KeyManager[].class), any(TrustManager[].class), any(SecureRandom.class));

If you want to simulate a KeyManagementException throwing, you can do as follows:

PowerMockito.doThrow(new KeyManagementException()).when(sslContextMock).init(any(KeyManager[].class), any(TrustManager[].class), any(SecureRandom.class));

Hope it helps.

troig
  • 7,072
  • 4
  • 37
  • 63
  • Thanks a lot. By the way, how can verify the output error message for the exception throwing. – houchker Oct 19 '14 at 22:32
  • In my case this solution is not working and giving error `org.mockito.exceptions.misusing.NotAMockException: Argument should be a mock, but is: class java.lang.Class` for line `PowerMockito.when(SSLContext.getInstance(anyString())).thenReturn(sslContextMock);`. Could you please help ? – PAA Jul 05 '20 at 15:23