3

I know this has been asked before but my case seems quite unique, I just upgraded to JDK 11 on one of my android projects, as a result, had to update Mockito and PowermMckito as well. Since then I'm facing this problem.

Argument should be a mock, but is: class java.lang.Class
org.mockito.exceptions.misusing.NotAMockException: Argument should be a mock, but is: class java.lang.Class
    at LogUtils.logException(LogUtils.java:84)
    at PushNotificationUtilsTest.setUp(PushNotificationUtilsTest.kt:23)
    at org.junit.internal.runners.MethodRoadie.runBefores(MethodRoadie.java:133)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:96)
    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.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.runTestClass(JUnitTestClassExecutor.java:110)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:58)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:38)
    at org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:62)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:33)
    at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:94)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:121)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:182)
    at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:164)
    at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:414)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
    at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:48)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56)
    at java.base/java.lang.Thread.run(Thread.java:834)

LogUtils is a class with static methods.

public final class LogUtils {
    
    private LogUtils() {
    }

 
    public static void logException(Throwable throwable) {
        if (BuildConfig.DEBUG) {
            throwable.printStackTrace();
        }

        FirebaseCrashlytics.getInstance().recordException(throwable);
    }

}

On the setup method.

@RunWith(PowerMockRunner::class)
@PrepareForTest(LogUtils::class)
class PushNotificationUtilsTest {

    @Before
    fun setUp() {
        PowerMockito.spy(LogUtils::class.java)
        PowerMockito.doNothing().`when`(LogUtils::class.java, "logException", any())
    }

    @Test
    fun isOneSignalNotification_withoutCustomKey() {
        val data = HashMap<String, String>()

        val result = PushNotificationUtils.isOneSignalNotification(data)

        assertThat(result).isFalse()
    }

    @Test
    fun isOneSignalNotification_withCustomKey() {
        val data = HashMap<String, String>()
        data["custom"] = "How's the josh?"

        val result = PushNotificationUtils.isOneSignalNotification(data)

        assertThat(result).isTrue()
    }

    @Test
    fun hasCorrectOneSignalPayload_withIncorrectPayload() {
        val data = HashMap<String, String>()
        data["custom"] = "{\"a\":{\"message\":{\"data\":{}}}}"

        val result = PushNotificationUtils.hasCorrectOneSignalPayload(data)

        assertThat(result).isFalse()
    }

    @Test
    fun hasCorrectOneSignalPayload_withCorrectPayload() {
        val data = HashMap<String, String>()
        data["custom"] = "{\"a\":{\"data\":{\"data\":{}}}}"

        val result = PushNotificationUtils.hasCorrectOneSignalPayload(data)

        assertThat(result).isTrue()
    }

    @Test
    fun getOneSignalData() {
        val data = HashMap<String, String>()
        data["custom"] = "{\"a\":{\"data\":{}}}"

        val result = PushNotificationUtils.getOneSignalPayload(data)

        val expectedData = JsonObject()
        assertThat(result).isEqualTo(expectedData)
    }
}

I remotely have no idea what the error is and how to fix it, tried all the StackOverflow questions and Github issues.

Arjun Manoj
  • 33
  • 1
  • 7
  • Can you please how us any `@Runner` or `@PrepareForTest` annotations on your test class? (I believe you should be using `PowerMockRunner` and preparing `LogUtils`.) – Jeff Bowman Nov 16 '21 at 21:22
  • @JeffBowman yes, I'm using the runner, I'll post the whole test class. – Arjun Manoj Nov 17 '21 at 04:35
  • I use Mockito, not PowerMockito, and on Java, not Kotlin and Android, but generally you are not supposed to mix matchers and values in expectations. This line in particular looks suspicious to me: `\`when\`(LogUtils::class.java, "logException", any())`. It should probably be something like `eq(LogUtils::class.java), eq("logException"), any()`. And similarly for any other `when`s that mix and match actual values with matchers. – David Conrad Nov 17 '21 at 04:48
  • 1
    @David You're right about not mixing matchers and non-matchers, but that call is actually a PowerMock-specific [2+ argument `when` method](https://www.javadoc.io/doc/org.powermock/powermock-api-mockito/1.7.0/org/powermock/api/mockito/expectation/PowerMockitoStubber.html) that is being called with matchers correctly. However, that overload is meant for static private static methods, while PowerMockito [suggests another syntax for static methods](https://github.com/powermock/powermock/wiki/Mockito#how-to-stub-void-static-method-to-throw-exception). I haven't repro'ed to be sure it's that though. – Jeff Bowman Nov 17 '21 at 06:45
  • @JeffBowman I'm using "org.mockito:mockito-core:3.4.4", "org.mockito:mockito-inline:3.4.4", "org.powermock:powermock-api-mockito2: 2.0.5" would this help? – Arjun Manoj Nov 17 '21 at 07:11
  • 1
    @JeffBowman i referred this GitHub issue: https://github.com/powermock/powermock/issues/992 but it did solve some of my failing test using `thenAnswer`, but not this particular one. – Arjun Manoj Nov 17 '21 at 09:21

0 Answers0