kotlinmockitomockito-kotlin

Kotlin mock is not a mock


I am creating a mock

private val mockTaskFactory = mock<TaskFactory>()

and then using it in a whenever block:

@BeforeEach fun setupTaskFactory() {
  doAnswer { ... }.whenever(mockTaskFactory).create(any<UUID>(), any<Boolean>(), any<Int>())
}

but I get told that mockTaskFactory is not a mock.

org.mockito.exceptions.misusing.NotAMockException: 
Argument passed to when() is not a mock!
Example of correct stubbing:
    doThrow(new RuntimeException()).when(mock).someMethod();

If I use a debugger I do indeed see a real instance of TaskFactory in mockTaskFactory at the start of the setupTaskFactory function.


Solution

  • I had Mockito.framework().clearInlineMocks() in another @BeforeEach function.

    I've replaced that with this:

    @JvmStatic
    @AfterAll
    fun cleanUpMocks() = Mockito.framework().clearInlineMocks()