运行以下测试时出现错误。
@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.
}
}
错误是
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!
我不确定为什么它说模拟豆不是模拟。有人能帮我弄明白吗?
您在一个测试中使用了2个mock框架。您需要使用属于用于构造mock的框架的验证。
核对MockK指南:
5
// Mockito
val mockedFile = mock(File::class.java)
mockedFile.read()
verify(mockedFile).read()
MockK:
// MockK
val mockedFile = mockk<File>()
mockedFile.read()
verify { mockedFile.read() }