We want to write tests for a project that uses Spring Boot and Spring AMQP. As we code in Kotlin we would like to use MockK instead of Mockito as it better fits Kotlin code style and best practices.
The RabbitListenerTestHarness
class provides some convienient feature for testing @RabbitListener
s. However, it returns implementations of Mockito's Answer
interface, which are incompatible with the Answer
interface of MockK.
Is there a way to use the Mockito answers with MockK, e.g. some exisiting wrappers for interoperability?
Consider the following example listener:
class SampleListener {
@RabbitListener(id = "sampleReceiver", queues = ["testQueue"])
fun receiveMessage(message: Message) {
}
}
and the actual test:
@SpringBootTest
class SampleTest(@Autowired val template: TestRabbitTemplate) {
@Autowired
lateinit var testHarness: RabbitListenerTestHarness
@Test
fun testRabbit() {
val spy = testHarness.getSpy<SampleListener>("sampleReceiver")
val answer: LatchCountDownAndCallRealMethodAnswer = testHarness.getLatchAnswerFor("sampleReceiver", 1)
// Mockito.doAnswer(answer).`when`(spy).receiveMessage(ArgumentMatchers.any())
every { spy.receiveMessage(any()) } answers { /* what goes here? */ }
template.convertAndSend("testQueue", "test")
}
}
The test contains the Mockito call, as mentioned in the Docs, as comment.
My question is, how can I use the answer object, returned from getLatchAnswerFor
to complete the MockK stub?