JAX-RS Test Extension ![badge](https://camo.githubusercontent.com/8853b94732f8d68cc7f74182f702484f443cc183d5450b90e2a5883c1f270b04/68747470733a2f2f6d6176656e2d6261646765732e6865726f6b756170702e636f6d2f6d6176656e2d63656e7472616c2f636f6d2e6769746875622e74312f6a61782d72732d746573742d657874656e73696f6e2f62616467652e737667)
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
-
There is no dependency injection, etc. You’ll have to pass in fully built JAX-RS service instances.
-
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.