Post
Simplifying JMS testing with embeddable brokers
Ever hoped for an easy way to run embed brokers and simulate a full cycle JMS messaging cycle? Well, this pet project of mine may be useful for you.
Ever hoped for an easy way to run embed brokers and simulate a full cycle JMS messaging cycle? Well, this pet project of mine may be useful for you (or, at least, give you an idea for an implementation). It works on top of JUnit, by providing a JUnit test runner, a JMS provider, a set of JMS-related annotations that will inject object instances and additional utility classes to simplify dealing with JMS. At the moment it works only with ActiveMQ, but adding additional providers should be fairly easy.
Here's example of the a test class that sends and receives data through an embeddable ActiveMQ broker:
@RunWith(JmsTestRunner.class)
Provider(
value = ActiveMqProvider.class,
configuration = ActiveMqConfiguration.class)
public class RequestReplyStringTest extends AbstractRequestReply<TextMessage> {
@Consumer(address = Defaults.REPLY_TO_QUEUE)
private MessageConsumer consumer;
@Producer
private MessageProducer producer;
@JmsSession
private Session session;
@Listener
private ServerListener listener;
@Test
public void testSendReceiveText() throws JMSException {
Destination replyTo = session.createQueue(Defaults.REPLY_TO_QUEUE);
Message request = session.createTextMessage("marco");
request.setJMSReplyTo(replyTo);
// Sets the class that will be instantiated to build the response
request.setStringProperty(ServerListener.REPLY_BUILDER,
StringReplyBuilder.class.getName());
producer.send(request);
Message response = consumer.receive(1000 * 5);
// handle the response
}
}
It's pretty simple as of now, but I can see some interesting uses for running certain types of tests.
You can find the project page here.