简体   繁体   中英

Mockk verify is trying to verify Log.d() which is never been called

So, I have written unit test as follows. Which basically calls a method in Viewmodel class.

    @Test
    fun `on clear Cached Calls AppUtility ClearCache`() {
        sut.clearCache()
        verify(exactly = 1) {
            appUtilityMock.clearCache()
        }
    }

Method in Viewmodel

    fun clearCache() {
        Log.d(TAG, "clearCache:e ")
        avonUtility.clearCache()
    }

As you can see, I am trying ti verify if clearCache() function is called.

And when I run the test I get following error. And it works(successful) as soon as I delete the line

Log.d(TAG, "clearCache:e ") .

Error stack trace

java.lang.RuntimeException: Method d in android.util.Log not mocked. See http://g.co/androidstudio/not-mocked for details.
    at android.util.Log.d(Log.java)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at io.mockk.proxy.jvm.advice.MethodCall.call(MethodCall.kt:14)
    at io.mockk.proxy.jvm.advice.SelfCallEliminatorCallable.call(SelfCallEliminatorCallable.kt:14)
    at io.mockk.impl.instantiation.JvmMockFactoryHelper.handleOriginalCall(JvmMockFactoryHelper.kt:83)
    at io.mockk.impl.instantiation.JvmMockFactoryHelper.access$handleOriginalCall(JvmMockFactoryHelper.kt:20)
    at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1$invocation$$inlined$stdFunctions$lambda$1.invoke(JvmMockFactoryHelper.kt:28)
    at io.mockk.impl.stub.MockKStub$handleInvocation$originalPlusToString$1.invoke(MockKStub.kt:227)
    at io.mockk.impl.stub.SpyKStub.defaultAnswer(SpyKStub.kt:15)
    at io.mockk.impl.stub.MockKStub.answer(MockKStub.kt:42)
    at io.mockk.impl.recording.states.AnsweringState.call(AnsweringState.kt:16)
    at io.mockk.impl.recording.CommonCallRecorder.call(CommonCallRecorder.kt:53)
    at io.mockk.impl.stub.MockKStub.handleInvocation(MockKStub.kt:263)
    at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1.invocation(JvmMockFactoryHelper.kt:25)
    at io.mockk.proxy.jvm.advice.Interceptor.call(Interceptor.kt:20)
    at android.util.Log.d(Log.java)

I have mock AppUtility like private val appUtilityMock: AppUtility = mockk(relaxed = true) .

I am not sure what is going wrong. Please help me understand. I am using MockK for testing.

You are calling the function Log.d , but this is in android.util.Log and therefore not available in your tests. See the documentation for more information. You can add a default implementation by adding this to your gradle file in the android { part:

testOptions { 
   unitTests.returnDefaultValues = true
}

You could also inject a wrapper around your Log and mock that one, like you do with AppUtility .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM