Artemis resources can be run inside JUnit Tests by using provided Rules (for JUnit 4) or Extensions (for JUnit 5). This can make it easier to embed messaging functionality in your tests.

These are provided by the packages artemis-junit (JUnit 4) and artemis-junit-5 (JUnit 5).

1. Examples

1.1. JUnit 4

1.1.1. Add Maven dependency

  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.37.0</version>
  6. <scope>test</scope>
  7. </dependency>

1.1.2. 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 server = new EmbeddedActiveMQResource();
  7. @Test
  8. public void myTest() {
  9. // test something, eg. create a queue
  10. server.createQueue("test.adress", "test.queue");
  11. }
  12. }

1.2. JUnit 5

1.2.1. Add Maven dependency

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

1.2.2. Declare a rule on your JUnit Test

  1. import org.apache.activemq.artemis.junit.EmbeddedActiveMQExtension;
  2. import org.junit.jupiter.api.Test;
  3. import org.junit.jupiter.api.extension.RegisterExtension;
  4. public class MyTest {
  5. @RegisterExtension
  6. public EmbeddedActiveMQExtension server = new EmbeddedActiveMQExtension();
  7. @Test
  8. public void myTest() {
  9. // test something, eg. create a queue
  10. server.createQueue("test.adress", "test.queue");
  11. }
  12. }

2. Ordering rules / extensions

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

2.1. JUnit 4

  1. import org.junit.Rule;
  2. import org.junit.rules.RuleChain;
  3. public EmbeddedActiveMQResource server = new EmbeddedActiveMQResource();
  4. public ActiveMQDynamicProducerResource producer = new ActiveMQDynamicProducerResource(server.getVmURL());
  5. @Rule
  6. public RuleChain ruleChain = RuleChain.outerRule(server).around(producer);

2.2. JUnit 5

  1. import org.junit.jupiter.api.Order;
  2. import org.junit.jupiter.api.extension.RegisterExtension;
  3. @RegisterExtension
  4. @Order(1)
  5. public EmbeddedActiveMQExtension producer = new EmbeddedActiveMQExtension();
  6. @RegisterExtension
  7. @Order(2)
  8. public ActiveMQDynamicProducerExtension producer = new ActiveMQDynamicProducerExtension(server.getVmURL());

3. Available Rules / Extensions

JUnit 4 RuleJUnit 5 ExtensionDescription

EmbeddedActiveMQResource

EmbeddedActiveMQExtension

Run a Server, without the JMS manager

EmbeddedJMSResource (deprecated)

no equivalent

Run a Server, including the JMS Manager

ActiveMQConsumerResource

ActiveMQConsumerExtension

Automate the creation of a consumer

ActiveMQDynamicProducerResource

ActiveMQDynamicProducerExtension

Automate the creation of a producer

ActiveMQProducerResource

ActiveMQProducerExtension

Automate the creation of a producer