jax-rs-test-extension

JUnit-5 extension to help testing JAX-RS infrastructure classes

License

License

GroupId

GroupId

com.github.t1
ArtifactId

ArtifactId

jax-rs-test-extension
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

jax-rs-test-extension
JUnit-5 extension to help testing JAX-RS infrastructure classes
Project URL

Project URL

https://github.com/t1/jax-rs-test-extension
Source Code Management

Source Code Management

https://github.com/t1/jax-rs-test-extension.git

Download jax-rs-test-extension

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.t1/jax-rs-test-extension/ -->
<dependency>
    <groupId>com.github.t1</groupId>
    <artifactId>jax-rs-test-extension</artifactId>
    <version>1.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.t1/jax-rs-test-extension/
implementation 'com.github.t1:jax-rs-test-extension:1.0.1'
// https://jarcasting.com/artifacts/com.github.t1/jax-rs-test-extension/
implementation ("com.github.t1:jax-rs-test-extension:1.0.1")
'com.github.t1:jax-rs-test-extension:jar:1.0.1'
<dependency org="com.github.t1" name="jax-rs-test-extension" rev="1.0.1">
  <artifact name="jax-rs-test-extension" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.t1', module='jax-rs-test-extension', version='1.0.1')
)
libraryDependencies += "com.github.t1" % "jax-rs-test-extension" % "1.0.1"
[com.github.t1/jax-rs-test-extension "1.0.1"]

Dependencies

compile (4)

Group / Artifact Type Version
io.undertow : undertow-core jar 2.2.5.Final
org.jboss.resteasy : resteasy-jaxrs jar 3.15.1.Final
org.jboss.resteasy : resteasy-undertow jar 3.15.1.Final
org.jboss.resteasy : resteasy-json-binding-provider jar 3.15.1.Final

provided (3)

Group / Artifact Type Version
org.projectlombok : lombok Optional jar 1.18.10
org.junit.jupiter : junit-jupiter jar 5.7.1
javax.ws.rs : javax.ws.rs-api jar 2.1.1

test (1)

Group / Artifact Type Version
org.assertj : assertj-core jar 3.19.0

Project Modules

There are no modules declared in this project.

JAX-RS Test Extension badge

Important
WunderBar might be a better fit for your use-case.

Very simple (and fast) JUnit-5 extension to test JAX-RS infrastructure classes. Uses RestEasy + Undertow internally, but you shouldn’t notice that too often; just stick to JAX-RS + JSON-B.

There are two use-cases:

1. Boundaries

These are your classes that provide a REST service using JAX-RS server API; i.e.:

@Path("/") public class MyBoundary {
    @GET public String get() { return "foo"; }
}

Could be tested like this:

public class MyBoundaryTest {

    @RegisterExtension static JaxRsClientTest jaxRs = new JaxRsClientTest(new MyBoundary());

    @Test public void shouldGet() {
        Response response = jaxRs.GET("/");

        assertThat(response.getStatusInfo()).isEqualTo(OK);
        assertThat(response.readEntity(String.class)).isEqualTo("foo");
    }
}

2. Gateways

These are your classes that consume a REST service using the JAX-RS client API, and you don’t want to integrate with the real external service; i.e.:

public class MyGateway {
    Client client;
    URI baseUri;

    public String getFoo() {
        Response response = client.target(baseUri).request(TEXT_PLAIN_TYPE).get();
        assert 200 == response.getStatus();
        return response.readEntity(String.class);
    }
}

Could be tested like this:

public class MyGatewayTest {
    @Path("/") public static class MockService {
        @GET public String get() { return "foo"; }
    }

    @RegisterExtension static JaxRsTestExtension jaxRs = new JaxRsTestExtension(new MockService());

    @Test void shouldGet() {
        MyGateway gateway = new MyGateway();
        gateway.client = jaxRs.client();
        gateway.baseUri = jaxRs.baseUri();

        String foo = gateway.getFoo();

        assertThat(foo).isEqualTo("foo");
    }
}

Limitations

  1. There is no dependency injection, etc. You’ll have to pass in fully built JAX-RS service instances.

  2. This is not for integration tests! E.g. it uses it’s own Application class. These test qualify as a unit tests: they only test your Boundary or Gateway in isolation.

Versions

Version
1.0.1