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

huangapple go评论53阅读模式
英文:

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?

英文:

I get an error when running the below test.

@ExtendWith(MockKExtension::class)
class SenderServiceTest {

   @MockK
   lateinit var kafkaService: KafkaService&lt;KeyType, MessageType&gt;


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

@Service
@ConditionalOnProperty(name = [&quot;kafka.enabled&quot;])
class KafkaService&lt;K, V&gt;(val producerFactory: ProducerFactory&lt;K, V&gt;, 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?

答案1

得分: 1

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&lt;File&gt;()

mockedFile.read()
verify { mockedFile.read() }
英文:

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&lt;File&gt;()

mockedFile.read()
verify { mockedFile.read() }

huangapple
  • 本文由 发表于 2023年4月6日 22:57:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75950955.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定