JMS - neilcatalan/MUnit-Implementation GitHub Wiki
For JMS implementation, I make use of the embedded ActiveMQ message broker. The benefit for this is that I don't need to have a complex mock definition and the test will be more close to reality. Below is the spring bean for starting the embedded ActiveMQ, it will automatically start by the mule or spring framework I believe.
<spring:beans>
<spring:bean name="amqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<spring:property name="brokerURL" value="${jms.borker.url}"/>
</spring:bean>
</spring:beans>
It is important that this dependency is included on your pom.xml:
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.13.2</version>
</dependency>
The JMS implementation here is consists of two flows, One with the inbound endpoint and the other has outbound endpoint:
<flow name="jmsInboundFlow">
<jms:inbound-endpoint queue="${jms.queue}" connector-ref="JMS_Connector_Configuration" doc:name="JMS_Listener"/>
<logger message="#["PRINT JMS INBOUND PAYLOAD: "+payload]" level="INFO" doc:name="Logger"/>
</flow>
<flow name="jmsOutboundFlow">
<jms:outbound-endpoint queue="${jms.queue}" connector-ref="JMS_Connector_Configuration" doc:name="JMS_Sender"/>
<logger level="INFO" doc:name="Logger" message="#["PRINT JMS OUTBOUND PAYLOAD: "+payload]"/>
</flow>
The test suite consists of three test cases see below config file:
<munit:config mock-inbounds="false" mock-connectors="false" doc:name="MUnit configuration"/>
<mclient:config name="Mule_Client" doc:name="Mule Client" stopPollsByDefault="true"/>
<spring:beans>
<spring:import resource="classpath:globalconfig.xml"/>
<spring:import resource="classpath:jms.xml"/>
</spring:beans>
<munit:test name="jms-test-Inbound-using-mule-client-suiteTest" description="MUnit Test">
<munit:set payload="#["This is a sample inbound message1"]" doc:name="Set Message"/>
<mclient:dispatch config-ref="Mule_Client" path="jms://${jms.queue}" doc:name="Mule Client" payload-ref="#[payload]"/>
</munit:test>
<munit:test name="jms-test-Inbound-using-jms-outbound-suiteTest" description="MUnit Test">
<munit:set payload="#["This is a sample inbound message2"]" doc:name="Set Message"/>
<jms:outbound-endpoint queue="${jms.queue}" connector-ref="JMS_Connector_Configuration" doc:name="JMS"/>
</munit:test>
<munit:test name="jms-test-Outbound-and-Inbound-suiteTest" description="Test">
<munit:set payload="#["This is a sample outbound message"]" doc:name="Set Message"/>
<flow-ref name="jmsOutboundFlow" doc:name="jmsOutboundFlow"/>
</munit:test>
It is important to take note that mock-inbounds and mock-connectors are set false, so that we can call the inbound endpoints and other connectors. The first test case dispatch a message to the inbound endpoint using the Mule Client. The second test case is similar to the first, the only difference is that it make use of JMS outbound endpoint. The last one calls the other flow with the outbound JMS connector which will send a message to the queue, expect that the inbound queue will be triggered here as well since it is still listening on the same queue.