Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.mockito.exceptions.misusing.NotAMockException: Argument should be a mock, but is: class java.lang.Class for Andriod SSLContext.getInstance() metho

I am working on Android and PowerMock project. In this example, I am trying to mock the below method, but I am getting error

org.mockito.exceptions.misusing.NotAMockException: Argument should be a mock, but is: class java.lang.Class

    at com.ABCTest.testTrustAppCertificates(ABCTest.java:314)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitS

Code:

private void trustAppCertificates(boolean isTrustAll) {
    try {
        TrustManager[] trustAllCerts = // get the Trust Manager 
        SSLContext sc = SSLContext.getInstance("TLSv1.2"); // Line-4
        sc.init(null, trustAllCerts, new SecureRandom()); // Line-5
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        HttpsURLConnection.setDefaultHostnameVerifier((arg0, arg1) -> isTrustAll);
    } catch (Exception e) {
        // LOG ERROR HERE
    }
}

Test case:

@PowerMockIgnore({"org.apache.http.conn.ssl.*", "javax.net.ssl.*" , "javax.crypto.*"})
@Test
public void testTrustAppCertificates() throws Exception {
    ...
    ...
    ...
    PowerMockito.mockStatic(SSLContext.class);
    SSLContext sslContextMock = mock(SSLContext.class);
    PowerMockito.doNothing().when(sslContextMock).init(any(KeyManager[].class), any(TrustManager[].class), any(SecureRandom.class));

    PowerMockito.when(SSLContext.getInstance(anyString())).thenReturn(sslContextMock); // This line breaks and gives the error mentioned above
    when(sslContextMock.getSocketFactory()).thenReturn(sSLSocketFactoryMock);

    PowerMockito.verifyPrivate(mockClass).invoke("trustAppCertificates", true);
}
like image 792
PAA Avatar asked Jul 05 '20 15:07

PAA


2 Answers

You can solve the issue using the below code. Hope this will help you. Please replace below the line

PowerMockito.when(SSLContext.getInstance(anyString())).thenReturn(sslContextMock);

with

when(SSLContext.getInstance(anyString())).thenAnswer((Answer<SSLContext>) invocation -> sslContextMock);
like image 71
Shrut.nin Avatar answered Oct 15 '22 10:10

Shrut.nin


Looks like the suggested Edit queue is full, otherwise I would make an edit to https://stackoverflow.com/a/62745095/10427291

This error also happens with this syntax:

doNothing().when(...);

As above just change the syntax to

when(...).thenAnswer((<AnyClass>) invocation -> null);

Seems like a hack to be able to throw in any object in the expected answer but null works for any object so that shouldn't be a problem. It's good enough for me for now.

like image 31
janst Avatar answered Oct 15 '22 11:10

janst