arve
02/12/2025, 9:24 PM// Subscribe to the bus to capture the message
val messages = mutableListOf<MessageStaffCommand>()
messageBus.subscribe<Foo>("topics/foo/") { messages += it }
// Act
messagebus.publish("topics/foo/", fooPayload)
// Assert
eventually(1.seconds) { messages.single() shouldBe payload }
It irks me slightly that I have to “extract” the message to a temp. variable in the callback. Of course i could assert in the callback directly but the test wouldn’t fail if something were wrong with message routing / topic mismatch, because the lambda would never be invoked.Emil Kantis
02/13/2025, 10:21 AMinterface QueueConsumer<out T> {
val value: List<T>
}
public inline operator fun <T> QueueConsumer<T>.getValue(
thisRef: Any?,
property: KProperty<*>,
): List<T> = value
inline fun <reified T> subscribingTo(topic: String): QueueConsumer<T> {
val messages = mutableListOf<T>()
messageBus.subscribe<T>(topic) { messages += it }
return object : QueueConsumer<T> {
override val value: List<T>
get() = messages
}
}
Then from your test
// Subscribe to the bus to capture the message
val messages by subscribingTo("topics/foo/")
// Act
messagebus.publish("topics/foo/", fooPayload)
// Assert
eventually(1.seconds) { messages.single() shouldBe payload }
Haven't tried it out really so can't promise it'll work flawlessly. 🙂arve
02/14/2025, 11:34 AM