Unit testing Active MQ

In the Apache Active MQ web page you can find some recommendations when unit testing JMS (ActiveMQ provides all the features from the Java Message Service specification and adds many more):

  • Use an embedded broker to avoid a separate broker process being required
  • Disable broker persistence so that no queue purging is required before/after tests
  • It’s often simpler and faster to just use Java code to create the broker via an XML configuration file using Spring etc.

In this test I will be also using Apache Camel to route the message from a socket to an Mock Endpoint through a queue. Previously, I have written a post where I explain how to unit testing Camel, which you can find it here.

First of all, we need to override the createCamelContext and create an ActiveMQConnectionFactory. This component will use an embedded broker, and we are going to disable the broker persistence setting the property broker.persistent to false.

protected CamelContext createCamelContext() throws Exception {
CamelContext camelContext = super.createCamelContext();

ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
camelContext.addComponent("activemq", jmsComponentClientAcknowledge(connectionFactory));

return camelContext;


Right after, we have to specify the routes in the createRouteBuilder method. In this case, I have created the endpoint where the messages will be coming through (localhost:6666). Note that Camel use the following syntax component:route. For this reason, the name of the component has to be the same as the name we have previously defined in the createCamelContext method (“activemq”).

protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {

public void configure() throws Exception {



Finally, I have created a test which will be successful if the HL7 text messaged sent is received correctly.

public void testSendHL7Message() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:end");

String m = "MSH|^~\\&|hl7Integration|hl7Integration|||||ADT^A01|||2.5|\r" +
"EVN|A01|20130617154644\r" +
"PID|1|465 306 5961||407623|Wood^Patrick^^^MR||19700101|1|\r" +


template.sendBody("mina:tcp://localhost:6666?textline=true&sync=false", m);


Please, find the entire unit test here.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *