| <div class="wiki-content maincontent"><p>When unit testing code with JMS you'll typically want to avoid the overhead of running separate proceses; plus you'll want to increase startup time as fast as possible as you tend to run unit tests often and want immediate feedback. Also persistence can often cause problems - as previous test case results can adversely affect future test case runs - so you often need to purge queues on startup.</p><p>So when unit testing JMS code we recommend the following</p><ul><li>Use <link><page ri:content-title="How do I embed a Broker inside a Connection"></page><plain-text-link-body>an embedded broker</plain-text-link-body></link> to avoid a separate broker process being required.</li><li>Disable <link><page ri:content-title="Broker Configuration URI"></page><plain-text-link-body>broker persistence</plain-text-link-body></link> so that no queue purging is required before/after tests.</li><li>It's often simpler and faster to just use Java code to create the broker via an XML configuration file using Spring etc.</li></ul><p>You can do all of this using the following Java code to create your JMS <strong><code>ConnectionFactory</code></strong> which will also automatically create an embedded broker</p><structured-macro ac:macro-id="964aef1a-20fb-4432-a94d-0c00a5328ca4" ac:name="code" ac:schema-version="1"><plain-text-body>ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false"); |
| </plain-text-body></structured-macro><p>For more configuration options see the <link><page ri:content-title="VM Transport Reference"></page></link> and the <link><page ri:content-title="Broker Configuration URI"></page></link></p><p>Or if you really would rather be more explicit you can create the broker first using the following Java code</p><structured-macro ac:macro-id="e0b3331e-ce0f-494e-8899-e62b59709eea" ac:name="code" ac:schema-version="1"><plain-text-body>BrokerService broker = new BrokerService(); |
| broker.setPersistent(false); |
| broker.start(); |
| </plain-text-body></structured-macro><p>or you could use <link><page ri:content-title="Spring Support"></page><plain-text-link-body>Spring Support.</plain-text-link-body></link></p><h3>Using JNDI</h3><p>If your application code is using JNDI to lookup the JMS <strong><code>ConnectionFactory</code></strong> and <strong><code>Destination</code></strong>'s to use, then you could use the <link><page ri:content-title="JNDI Support"></page></link> in ActiveMQ.</p><p>Add the following <strong><code>jndi.properties</code></strong> to your classpath, e.g., in <strong><code>src/test/resources</code></strong>, if you are using maven:</p><structured-macro ac:macro-id="17e1b668-d297-4975-950f-dbb2ef742cde" ac:name="code" ac:schema-version="1"><plain-text-body>java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory |
| java.naming.provider.url = vm://localhost?broker.persistent=false</plain-text-body></structured-macro><p>You should then consider using <link><page ri:content-title="JNDI Support"></page><plain-text-link-body>Dynamic destinations in JNDI</plain-text-link-body></link> so that your code looks up destinations via</p><structured-macro ac:macro-id="d2677683-9688-427e-9554-dc0eaf165251" ac:name="code" ac:schema-version="1"><parameter ac:name="language">java</parameter><plain-text-body>context.lookup("dynamicQueues/FOO.BAR");</plain-text-body></structured-macro><h3>Using The <code>EmbeddedActiveMQBroker</code> JUnit Rule (ActiveMQ 5.13)</h3><p>If your test code is using JUnit, then you could use the <strong><code>EmbeddedActiveMQBroker</code></strong> JUnit Rule provided in the <strong><code>activemq-junit</code></strong> library. Add the <strong><code>activemq-junit</code></strong> library along with the <strong><code>activemq-broker</code></strong> libraries for the version of ActiveMQ you want to test with.  The rule will use whatever version of ActiveMQ it finds in the classpath, so the ActiveMQ libraries need to be specified if they are not already there.</p><p>If you are using Maven, add the following to your <strong><code>pom.xml</code></strong>:</p><structured-macro ac:macro-id="6c23691e-68d8-4990-8b71-a922a216952e" ac:name="code" ac:schema-version="1"><parameter ac:name="language">xml</parameter><plain-text-body><dependency> |
| <groupId>org.apache.activemq.tooling</groupId> |
| <artifactId>activemq-junit</artifactId> |
| <version>${activemq-junit-version}</version> |
| <scope>test</scope> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.apache.activemq</groupId> |
| <artifactId>activemq-broker</artifactId> |
| <version>${activemq-version}</version> |
| <scope>test</scope> |
| </dependency></plain-text-body></structured-macro><p> Then add the <strong><code>EmbeddedActiveMQBroker</code></strong> JUnit Rule to your test, and JUnit will start the embedded broker at the beginning of each test and stop the broker at the end of the test.</p><structured-macro ac:macro-id="b5da19f2-ebc7-4557-a357-ff6f3d244afa" ac:name="code" ac:schema-version="1"><parameter ac:name="language">java</parameter><parameter ac:name="title">Use The ActiveMQ JUnit Rule</parameter><plain-text-body>@Rule |
| public EmbeddedActiveMQBroker broker = new EmbeddedActiveMQBroker();</plain-text-body></structured-macro><p>By default, the <strong><code>EmbeddedActiveMQBroker</code></strong> will configure the broker as non-persistent, and the only transport available will be the VM transport. To customize this configuration, either extend the <strong><code>EmbeddedActiveMQBroker</code></strong> class and override the <strong><code>configure()</code></strong> method, or use an XML configuration for the broker.  </p><structured-macro ac:macro-id="abacc4e2-d9bb-4b83-a7f3-301bf125c3e5" ac:name="info" ac:schema-version="1"><rich-text-body><strong>Note</strong>: to configure an <strong><code>EmbeddedActiveMQBroker</code></strong> using XML configuration, you may need to add additional libraries to the classpath to support XBean configuration of ActiveMQ.</rich-text-body></structured-macro><structured-macro ac:macro-id="232d811d-409b-45ba-a74f-999ba056d56c" ac:name="code" ac:schema-version="1"><parameter ac:name="language">java</parameter><parameter ac:name="title">Customizing An EmbeddedActiveMQBroker Using Java</parameter><plain-text-body>@Rule |
| EmbeddedActiveMQBroker customizedBroker = new EmbeddedActiveMQBroker() { |
| @Override |
| protected void configure() { |
| // Perform additional configuration here... |
| } |
| }</plain-text-body></structured-macro><p><span><br clear="none"></span></p><structured-macro ac:macro-id="cbaabf0f-fc90-4577-a2f5-d7605414c3db" ac:name="code" ac:schema-version="1"><parameter ac:name="language">java</parameter><parameter ac:name="title">Customizing An EmbeddedActiveMQBroker Using XML Configuration</parameter><plain-text-body>@Rule |
| EmbeddedActiveMQBroker customizedBroker = new EmbeddedActiveMQBroker("bean:customize-activemq.xml"); |
| </plain-text-body></structured-macro><p>Note that to use the XML configuration, you may need to add additional libraries on the classpath to support the XBean configuration of ActiveMQ.  The versions of the <strong><code>spring-context</code></strong> library should correspond with the version used by your selected version of ActiveMQ.</p><structured-macro ac:macro-id="4139db17-61be-4519-a250-924edb3b8dc3" ac:name="code" ac:schema-version="1"><parameter ac:name="language">xml</parameter><parameter ac:name="title">Maven Configuration For XBean Configuration</parameter><plain-text-body><dependency> |
| <groupId>org.springframework</groupId> |
| <artifactId>spring-context</artifactId> |
| <version>Appropriate version for activemq-version</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.apache.activemq</groupId> |
| <artifactId>activemq-spring</artifactId> |
| <version>${activemq-version></version> |
| </dependency></plain-text-body></structured-macro><p>Then you can use the VM URI to connect with the broker</p><structured-macro ac:macro-id="2e52bef4-907c-41ff-bf56-096812c967ac" ac:name="code" ac:schema-version="1"><parameter ac:name="language">java</parameter><plain-text-body>ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://embedded-broker?create=false");</plain-text-body></structured-macro><p>You can also get a connection factory from the <strong><code>EmbeddedActiveMQBroker</code></strong>:</p><structured-macro ac:macro-id="19cc19a7-79f8-4d09-9ac1-61b359a83769" ac:name="code" ac:schema-version="1"><parameter ac:name="language">java</parameter><plain-text-body>ConnectionFactory connectionFactory = embeddedBroker.createConnectionFactory();</plain-text-body></structured-macro></div> |
| |