2016-09-26 15:33:39 -04:00
# Unit Testing
2022-08-18 03:05:06 -04:00
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.
2016-09-26 15:33:39 -04:00
2022-08-18 03:05:06 -04:00
These are provided by the packages `artemis-junit` (JUnit 4) and `artemis-junit-5` (JUnit 5).
2016-09-26 15:33:39 -04:00
2022-08-18 03:05:06 -04:00
## Examples
2016-09-26 15:33:39 -04:00
2022-08-18 03:05:06 -04:00
### JUnit 4
#### Add Maven dependency
2016-09-26 15:33:39 -04:00
```xml
< dependency >
2018-03-09 10:07:38 -05:00
< groupId > org.apache.activemq< / groupId >
< artifactId > artemis-junit< / artifactId >
<!-- replace this for the version you are using -->
2022-08-18 03:05:06 -04:00
< version > @PROJECT_VERSION_FILTER_TOKEN@< / version >
2018-03-09 10:07:38 -05:00
< scope > test< / scope >
2016-09-26 15:33:39 -04:00
< / dependency >
```
2022-08-18 03:05:06 -04:00
#### Declare a rule on your JUnit Test
2016-09-26 15:33:39 -04:00
```java
2018-01-25 09:48:39 -05:00
import org.apache.activemq.artemis.junit.EmbeddedActiveMQResource;
2016-09-26 15:33:39 -04:00
import org.junit.Rule;
import org.junit.Test;
public class MyTest {
@Rule
2022-08-18 03:05:06 -04:00
public EmbeddedActiveMQResource server = new EmbeddedActiveMQResource();
2016-09-26 15:33:39 -04:00
@Test
public void myTest() {
2022-08-18 03:05:06 -04:00
// test something, eg. create a queue
server.createQueue("test.adress", "test.queue");
2016-09-26 15:33:39 -04:00
}
}
```
2022-08-18 03:05:06 -04:00
### JUnit 5
2016-09-26 15:33:39 -04:00
2022-08-18 03:05:06 -04:00
#### Add Maven dependency
```xml
< dependency >
< groupId > org.apache.activemq< / groupId >
< artifactId > artemis-junit-5< / artifactId >
<!-- replace this for the version you are using -->
< version > @PROJECT_VERSION_FILTER_TOKEN@< / version >
< scope > test< / scope >
< / dependency >
2016-09-26 15:33:39 -04:00
```
2022-08-18 03:05:06 -04:00
#### Declare a rule on your JUnit Test
```java
import org.apache.activemq.artemis.junit.EmbeddedActiveMQExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
public class MyTest {
@RegisterExtension
public EmbeddedActiveMQExtension server = new EmbeddedActiveMQExtension();
@Test
public void myTest() {
// test something, eg. create a queue
server.createQueue("test.adress", "test.queue");
}
}
2016-09-26 15:33:39 -04:00
```
2022-08-18 03:05:06 -04:00
## Ordering rules / extensions
This is actually a JUnit feature, but this could be helpful on pre-determining the order on which rules are executed.
### JUnit 4
2016-09-26 15:33:39 -04:00
```java
2022-08-18 03:05:06 -04:00
import org.junit.Rule;
import org.junit.rules.RuleChain;
public EmbeddedActiveMQResource server = new EmbeddedActiveMQResource();
public ActiveMQDynamicProducerResource producer = new ActiveMQDynamicProducerResource(server.getVmURL());
2016-09-26 15:33:39 -04:00
2018-03-09 10:07:38 -05:00
@Rule
2022-08-18 03:05:06 -04:00
public RuleChain ruleChain = RuleChain.outerRule(server).around(producer);
```
### JUnit 5
```java
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.extension.RegisterExtension;
@RegisterExtension
@Order (1)
public EmbeddedActiveMQExtension producer = new EmbeddedActiveMQExtension();
@RegisterExtension
@Order (2)
public ActiveMQDynamicProducerExtension producer = new ActiveMQDynamicProducerExtension(server.getVmURL());
2016-09-26 15:33:39 -04:00
```
2022-08-18 03:05:06 -04:00
## Available Rules / Extensions
2016-09-26 15:33:39 -04:00
2022-08-18 03:05:06 -04:00
JUnit 4 Rule | JUnit 5 Extension | Description
--- | --- | ---
EmbeddedActiveMQResource | EmbeddedActiveMQExtension | Run a Server, without the JMS manager
2023-02-10 13:06:06 -05:00
~~EmbeddedJMSResource~~ (deprecated) | no equivalent | Run a Server, including the JMS Manager
2022-08-18 03:05:06 -04:00
ActiveMQConsumerResource | ActiveMQConsumerExtension | Automate the creation of a consumer
ActiveMQDynamicProducerResource | ActiveMQDynamicProducerExtension | Automate the creation of a producer
ActiveMQProducerResource | ActiveMQProducerExtension | Automate the creation of a producer