https://github.com/baloise/resteasy-test
Test JAX-RS REST endpoints in JUnit without hassle
https://github.com/baloise/resteasy-test
Last synced: 13 days ago
JSON representation
Test JAX-RS REST endpoints in JUnit without hassle
- Host: GitHub
- URL: https://github.com/baloise/resteasy-test
- Owner: baloise
- License: mit
- Created: 2018-06-26T23:22:02.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-11-01T21:28:43.000Z (over 6 years ago)
- Last Synced: 2025-02-22T21:27:03.646Z (over 1 year ago)
- Language: Java
- Homepage:
- Size: 21.5 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ResteasyTest
ResteasyTest provides a convenient way to test JAX-RS resources in your JUnit test. It allows you to define your REST endpoints in your tests classes and verify that the exposed resources work properly. ResteasyTest is similar to [JerseyTest](https://github.com/jersey/jersey/blob/master/test-framework/core/src/main/java/org/glassfish/jersey/test/JerseyTest.java) but uses [Resteasy](https://resteasy.github.io/) as provider and is not as feature rich. The dependencies used are the same as in JBoss EAP / WildFly. These are [undertow](http://undertow.io/) as ServletContainer, [Resteasy](https://resteasy.github.io/) as JAX-RS implementation and [jackson 2](https://github.com/FasterXML/jackson) for JSON support.
# How does it work
ResteasyTest starts undertow using a free port on your system. In your JUnit test you define JAX-RS endpoints which are deployed to undertow. These endpoints can be tested with the methods `post()` and `get()` which use [ResteasyClient](https://docs.jboss.org/resteasy/docs/3.0-beta-3/userguide/html/RESTEasy_Client_Framework.html) to send the requests to the started undertow instance.
# Setup dependencies
Because ResteasyTest isn't available as Maven dependency in the Maven Central Repository, you need to include it in your project yourself. If demanded, I will make the effort to upload it.
To get ResteasyTest running, you need to include the following Maven dependencies in your `` section:
```
io.undertow
undertow-servlet
${undertow.version}
test
io.undertow
undertow-core
${undertow.version}
test
org.jboss.resteasy
resteasy-undertow
${resteasy.version}
test
org.jboss.resteasy
resteasy-jaxrs
${resteasy.version}
test
org.jboss.resteasy
resteasy-jackson2-provider
${resteasy.version}
test
```
To add JAXB annotation support, you might want to add these dependency too:
```
com.fasterxml.jackson.module
jackson-module-jaxb-annotations
${jackson.version}
test
```
For `java.time` support, you need to this dependency, if the used Jackson version is below 2.8.5. This is the case for JBoss EAP 7.0 or WildFly 10.
```
com.fasterxml.jackson.datatype
jackson-datatype-jsr310
${jackson.version}
test
```
If your Jackson version is above 2.8.5 (like in JBoss EAP 7.1+), you need to use the [jackson-modules-java8](https://github.com/FasterXML/jackson-modules-java8). This isn't tested yet.
### JBossEAP 7.0 / WildFly 10 dependency versions
For JBoss EAP 7.0 and WildFly 10 (upstream project) use the following dependency versions:
```
3.0.16.Final
1.3.21.Final
2.6.3
```
So you use the same artifacts and versions as in your application server.
### JBoss EAP 7.1 / WildFly 11 dependency versions
For JBoss EAP 7.1 and WildFly 11 (upstream project) use the these dependency versions. This setup isn't tested from me yet but should work. If you use additional Jackson modules, make sure these are compatible with these versions.
```
3.0.24.Final
1.3.25.Final
2.8.9
```
# Usage
To use ResteasyTest, you need to include [ResteasyTest.java](https://raw.githubusercontent.com/niiku/resteasy-test/master/src/main/java/io/nikio/jaxrs/ResteasyTest.java) in your maven module where you want to test a JAX-RS resource.
To test a JAX-RS endpoint, create a test class and inherit the now included `ResteasyTest.java` class. Override the method `configureResource()` to add an instance of your REST endpoint to ResteasyTest.
```
public class RestEndpointTest extends ResteasyTest {
@Override
public List configureResources() {
return Stream.of(new RestEndpoint()).collect(Collectors.toList());
}
}
```
To declare a provider (eg. for custom field mappings) override `configureProvider()`.
```
@Override
public List configureProvider() {
return Stream.of(new ObjectMapperContextResolver()).collect(Collectors.toList());
}
```
Now you can simply use `get()` and `post()` to test your endpoint:
```
@Test
public void testGetPojo() {
Pojo expectedPojo = new Pojo("Hello");
Pojo actualPojo = get("/resource/pojo", Pojo.class);
Assert.assertEquals(expectedPojo, actualPojo);
}
@Test
public void testPostPojo() {
Pojo world = new Pojo("World");
Pojo response = post("/resource/post", world, Pojo.class);
Assert.assertEquals("Hello World", response.getName());
}
```
To get the result without deserialization, simply put `String.class` as last argument to `post()` or `get()`. You find complete examples in [ResteasyTestTest.java](https://github.com/niiku/resteasy-test/blob/master/src/test/java/io/nikio/jaxrs/ResteasyTestTest.java).
# CDI Support
ResteasyTest doesn't support CDI directly. But you can use [cdi-unit](http://bryncooke.github.io/cdi-unit/) to inject a REST endpoint in your JUnit test and return it in the overriden `configureResources()` method. But for the most cases I would recommend using [mockito](http://site.mockito.org/) with its `MockitoJUnitRunner.class` to satisfy `@Inject` points. `cdi-unit` is slow because it's using [Weld](http://weld.cdi-spec.org/). I think the purpose should be to test your REST endpoints serialization/deserialization and URL only.