Mockk: Cannot verify a mocked class's method was not called.

Created on 4 Sep 2019  路  2Comments  路  Source: mockk/mockk

Prerequisites

  • [x] I am running the latest version (1.9.3)
  • [x] I checked the documentation and found no answer
  • [x] I checked to make sure that this issue has not already been filed

Context

I have not found a way to write an assertion verifying that a mocked class's function was not called. When I write the assertion it complains that the function was not stubbed. But that function was never called in my test or implementation.

Is there another way to write this test?

In the example below I expected both tests to pass but the 2nd test fails.

  • MockK version: 1.9.3
  • OS: macOS 10.14.5
  • Kotlin version: 1.3.50
  • JDK version: 11
  • JUnit version: N/A (Spek)
  • Type of test: unit test

Minimal reproducible code (the gist of this issue)

import io.mockk.Called
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe

class Adder {
    fun addOne(value: Int) = value + 1
    fun addTwo(value: Int) = value + 2
}

class BusinessMachine(private val adder: Adder) {
    fun doBusiness(): Int {
        val money = 100
        return adder.addOne(money)
    }
}

object BusinessMachineTest : Spek({
    describe("BusinessMachine") {
        it("should use addOne to do business") {
            val adder = mockk<Adder>()
            val subject = BusinessMachine(adder)
            every { adder.addOne(any()) } returns 1

            subject.doBusiness()

            verify { adder.addOne(any()) }
        }

        it("should not use addTwo to do business") {
            val adder = mockk<Adder>()
            val subject = BusinessMachine(adder)
            every { adder.addOne(any()) } returns 1

            subject.doBusiness()

            verify { adder::addTwo wasNot Called }
        }
    }
})

Failure Logs

can't find stub fun com.distribrewtion.api.users.mailchimp.Adder.addTwo(kotlin.Int): kotlin.Int
io.mockk.MockKException: can't find stub fun com.distribrewtion.api.users.mailchimp.Adder.addTwo(kotlin.Int): kotlin.Int
    at io.mockk.impl.stub.StubRepository.stubFor(StubRepository.kt:16)
    at io.mockk.impl.recording.states.VerifyingState.checkWasNotCalled(VerifyingState.kt:82)
    at io.mockk.impl.recording.states.VerifyingState.recordingDone(VerifyingState.kt:45)
    at io.mockk.impl.recording.CommonCallRecorder.done(CommonCallRecorder.kt:47)
    at io.mockk.impl.eval.RecordedBlockEvaluator.record(RecordedBlockEvaluator.kt:60)
    at io.mockk.impl.eval.VerifyBlockEvaluator.verify(VerifyBlockEvaluator.kt:30)
    at io.mockk.MockKDsl.internalVerify(API.kt:118)
    at io.mockk.MockKKt.verify(MockK.kt:139)
    at io.mockk.MockKKt.verify$default(MockK.kt:136)
    at com.distribrewtion.api.users.mailchimp.BusinessMachineTest$1$1$3.invoke(mockkexample.kt:52)
    at com.distribrewtion.api.users.mailchimp.BusinessMachineTest$1$1$3.invoke(mockkexample.kt:22)
    at org.spekframework.spek2.runtime.scope.TestScopeImpl.execute(Scopes.kt:94)
    at org.spekframework.spek2.runtime.Executor$execute$$inlined$executeSafely$lambda$1$1.invokeSuspend(Executor.kt:52)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:238)
    at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
    at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:742)

Most helpful comment

I think in your case you should use:
verify(exactly = 0) { adder.addTwo(any()) }

Construction like verify { mock wasNot Called } is used not for function but for whole mock

All 2 comments

I think in your case you should use:
verify(exactly = 0) { adder.addTwo(any()) }

Construction like verify { mock wasNot Called } is used not for function but for whole mock

Thanks @sgulyaev, that works for me.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

VictorAlbertos picture VictorAlbertos  路  4Comments

almozavr picture almozavr  路  3Comments

VictorAlbertos picture VictorAlbertos  路  5Comments

anticafe picture anticafe  路  4Comments

pschyma picture pschyma  路  3Comments