This is a simple project which allows a unit testing for Jakarta REST endpoints using the SeBootstrap
from
Jakarta REST 3.1.
To use this JUnit 5 integration you simply need to add a @RestBootstrap(YourApplication.class)
annotation to your test. A
SeBoostrap.Instance
will be created and started based on the implementation you choose.
The first dependency you need is the testing tooling.
<dependencies>
<dependency>
<groupId>dev.resteasy.testing.tools</groupId>
<artifactId>junit-testing-tools</artifactId>
<version>${version.dev.resteasy.testing.tools</version>
</dependency>
</dependencies>
Next you need to define an implementation. This project chose not to be implementation specific and should work with any Jakarta REST 3.1+ implementation.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-bom</artifactId>
<version>${version.org.jboss.resteasy}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>${version.org.junit}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-core-spi</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-undertow-cdi</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
@RestBootstrap(value = SimpleTest.TestApplication.class)
public class SimpleTest {
@Inject
@RequestPath("/test/echo")
private WebTarget webTarget;
@Test
public void invokeResource(final UriBuilder builder) {
try (Client client = ClientBuilder.newClient()) {
final String result = client.target(builder.path("/test/echo/"))
.request()
.post(Entity.text("Hello"), String.class);
Assertions.assertEquals("Hello", result);
}
}
@Test
public void invokeResourceOnInjectedClient() {
final String result = webTarget.request()
.post(Entity.text("Hello"), String.class);
Assertions.assertEquals("Hello", result);
}
@ApplicationPath("/test")
public static class TestApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
return Set.of(EchoResource.class);
}
}
@Path("/echo")
public static class EchoResource {
@POST
public String echo(String text) {
return text;
}
}
}
You can inject some types into your tests. For fields you use the @jakarta.inject.Inject
annotation. Constructor and
method parameters do not require the @Inject
annotation. The following types can be injected.
-
jakarta.ws.rs.SeBootstrap.Configuration
The configuration from the
SeBoostrap.Instance
that was started. -
jakarta.ws.rs.client.Client
A REST Client. This can have a qualifier of
@RestClientConfig
which returns aRestClientBuilderProvider
and allows the configuration to be overridden for the client. -
jakarta.ws.rs.core.UriBuilder
Injects a URI builder. Given this is a mutable builder, this should likely only be injected as a method parameter.
-
java.net.URI
The base URI for
SeBootstrap.Instance
that was started. -
jakarta.ws.rs.client.WebTarget
This can be used with the
@RequestPath
qualifier. It creates aWebTarget
from a configured client.
Type | Field | Parameter | Constructor | Qualifiers |
---|---|---|---|---|
|
X |
X |
X |
|
|
X |
X |
X |
|
|
X |
X |
||
|
X |
X |
X |
|
|
X |
X |
X |
|