Writing a test case for the Point-to-point messaging model

Let's write a test case to see this code in action.

We are actually going to write two test cases: one for the Producer class, which sends a message to the queue, and another one for the Consumer class, which retrieves the message from the queue.

First, let's write a test case for the Producer to simulate the sendMessage() function:

class ProducerTest {

@Test
fun sendMessageTest() {

val seContainerInitializer = SeContainerInitializer.newInstance()
val producer = seContainerInitializer.initialize()
.select(Producer::class.java)
.get()

Assert.assertNotNull(producer)
val message = producer.sendMessage("TEST MESSAGE")
Assert.assertEquals("Message sent", message)
}
}

When this test is executed, it pushes a TEST MESSAGE to the queue. The message waits in the queue for a consumer to consume it.

Let's now write a test for the Consumer to test the receiveMessage() function:

class ConsumerTest {

@Test
fun receiveMessageTest() {

val seContainerInitializer = SeContainerInitializer.newInstance()
val consumer = seContainerInitializer.initialize()
.select(Consumer::class.java)
.get()
Assert.assertNotNull(consumer)
val message = consumer.receiveMessage()
Assert.assertNotNull(message)
}
}

If we run these test cases, we can see that the sendMessage() function in the Producer class produces the message and pushes it into the queue. The receiveMessage() function in the Consumer class has consumed the message from the producer.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset