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.

[java]
@Override
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;
}
[/java]

 

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”).

[java]
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {

@Override
public void configure() throws Exception {

from(“mina:tcp://localhost:6666?textline=true&sync=false”)
.to(“activemq:processHL7”);

from(“activemq:processHL7”)
.to(“mock:end”);
}
};
}

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

[java]
@Test
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” +
“PV1|1||Location||||||||||||||||261938_6_201306171546|||||||||||||||||||||||||20130617134644|”;

mock.expectedBodiesReceived(m);

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

mock.assertIsSatisfied();
}
[/java]
Please, find the entire unit test here.

You may also like...

6 Responses

  1. mmy says:

    Mmy? Never heard of it until today. Gave the site a quick look. Seems legit, but need more time to form opinion. Let me know what you guys reckon! mmy

  2. Agregator RSS says:

    Zauważyłem, że często angażujesz się w interakcje z czytelnikami w komentarzach. Jakie korzyści widzisz w tym dialogu? Czy to wpływa na kierunek, w którym zmierza twój blog, czy może jest to sposób na budowanie silniejszej społeczności czytelników?

    https://xmc.pl

  3. Thanks for sharing. I read many of your blog posts, cool, your blog is very good.

  4. Thanks for sharing. I read many of your blog posts, cool, your blog is very good. https://accounts.binance.com/lv/register?ref=SMUBFN5I

  5. Prihlásení says:

    I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article. https://accounts.binance.info/register-person?ref=IHJUI7TF

  6. Your point of view caught my eye and was very interesting. Thanks. I have a question for you.

Leave a Reply to mmy Cancel reply

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