Unit Testing

The package artemis-junit provides tools to facilitate how to run Artemis resources inside JUnit Tests.

These are provided as JUnit “rules” and can make it easier to embed messaging functionality on your tests.

Example

Import this on your pom.xml

  1. <dependency>
  2. <groupId>org.apache.activemq</groupId>
  3. <artifactId>artemis-junit</artifactId>
  4. <!-- replace this for the version you are using -->
  5. <version>2.5.0</version>
  6. <scope>test</scope>
  7. </dependency>

Declare a rule on your JUnit Test

  1. import org.apache.activemq.artemis.junit.EmbeddedActiveMQResource;
  2. import org.junit.Rule;
  3. import org.junit.Test;
  4. public class MyTest {
  5. @Rule
  6. public EmbeddedActiveMQResource resource = new EmbeddedActiveMQResource();
  7. @Test
  8. public void myTest() {
  9. }
  10. }

This will start a server that will be available for your test:

  1. [main] 17:00:16,644 INFO [org.apache.activemq.artemis.core.server] AMQ221000: live Message Broker is starting with configuration Broker Configuration (clustered=false,journalDirectory=data/journal,bindingsDirectory=data/bindings,largeMessagesDirectory=data/largemessages,pagingDirectory=data/paging)
  2. [main] 17:00:16,666 INFO [org.apache.activemq.artemis.core.server] AMQ221045: libaio is not available, switching the configuration into NIO
  3. [main] 17:00:16,688 INFO [org.apache.activemq.artemis.core.server] AMQ221043: Protocol module found: [artemis-server]. Adding protocol support for: CORE
  4. [main] 17:00:16,801 INFO [org.apache.activemq.artemis.core.server] AMQ221007: Server is now live
  5. [main] 17:00:16,801 INFO [org.apache.activemq.artemis.core.server] AMQ221001: Apache ActiveMQ Artemis Message Broker version 2.5.0-SNAPSHOT [embedded-server, nodeID=39e78380-842c-11e6-9e43-f45c8992f3c7]
  6. [main] 17:00:16,891 INFO [org.apache.activemq.artemis.core.server] AMQ221002: Apache ActiveMQ Artemis Message Broker version 2.5.0-SNAPSHOT [39e78380-842c-11e6-9e43-f45c8992f3c7] stopped, uptime 0.272 seconds

Ordering rules

This is actually a JUnit feature, but this could be helpful on pre-determining the order on which rules are executed.

  1. ActiveMQDynamicProducerResource producer = new ActiveMQDynamicProducerResource(server.getVmURL());
  2. @Rule
  3. public RuleChain ruleChain = RuleChain.outerRule(new ThreadLeakCheckRule()).around(server).around(producer);

Available Rules

NameDescription
EmbeddedActiveMQResourceRun a Server, without the JMS manager
EmbeddedJMSResourceRun a Server, including the JMS Manager
ActiveMQConsumerResourceAutomate the creation of a consumer
ActiveMQProducerResourceAutomate the creation of a producer
ThreadLeakCheckRuleCheck that all threads have been finished after the test is finished