Search code examples
kotlinjunitmockitomockkmockk-verify

Argument passed to verify() is of type KafkaProducerService and is not a mock


I get an error when running the below test.

@ExtendWith(MockKExtension::class)
class SenderServiceTest {

   @MockK
   lateinit var kafkaService: KafkaService<KeyType, MessageType>


   @Test
   fun `Send message`() {
      val key = KeyType()
      val value = MessageType()
      verify(kafkaService).send(key, value)
   }
}

@Service
@ConditionalOnProperty(name = ["kafka.enabled"])
class KafkaService<K, V>(val producerFactory: ProducerFactory<K, V>, val names: KafkaNames) {

   fun send(key: K, value: V) {
     // some code to send the message.
   }

}

The error is

org.mockito.exceptions.misusing.NotAMockException: 
Argument passed to verify() is of type KafkaService and is not a mock!
Make sure you place the parenthesis correctly!

I am not sure why it says the mock bean is not a mock. Someone can help to figure it out?


Solution

  • You are using 2 mocking frameworks in one test. You need to use verify belonging to framework you used to construct the mock.

    Check verify in MockK guidebook:

    Mockito

    // Mockito
    val mockedFile = mock(File::class.java)
    
    mockedFile.read()
    verify(mockedFile).read()
    

    MockK:

    // MockK
    val mockedFile = mockk<File>()
    
    mockedFile.read()
    verify { mockedFile.read() }