Lightweight library that provides AssertJ custom assertions to JAX-RS responses.
Example:
try (Response response = target.path("/resource").request().get()) {
assertThat(response).hasStatusCode(Status.OK)
.hasMediaType(MediaType.APPLICATION_JSON_TYPE)
.entityAs(ExampleRepresentation.class)
.satisfies(e -> {
assertThat(e.getName()).isEqualTo("myname");
assertThat(e.getValue()).isEqualTo(10);
});
}
For more examples se integration tests:
Add the following dependency :
<dependency>
<groupId>com.github.attiand</groupId>
<artifactId>assertj-jaxrs-core</artifactId>
<version>${version}</version>
<scope>test</scope>
</dependency>
You also need a jaxrs client implementation, for example:
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<scope>test</scope>
</dependency>
Simple support for asserting JSON Pointer values if you don't have the entity type available:
try (Response response = target.path("/resource").request().get()) {
assertThat(response)
.hasStatusCode(Status.OK)
.entityAs(JSON)
.satisfies(e -> {
assertThat(e).pathValue("/name").asString().isEqualTo("name");
assertThat(e).pathValue("/value").asInteger().isEqualTo(10);
});
}
Add the following dependency:
<dependency>
<groupId>com.github.attiand</groupId>
<artifactId>assertj-jaxrs-json</artifactId>
<version>${version}</version>
<scope>test</scope>
</dependency>
You also need a javax.json-api
implementation, for example:
<dependency>
<groupId>org.eclipse</groupId>
<artifactId>yasson</artifactId>
<scope>test</scope>
</dependency>
try (Response response = target.path("/resource").request().get()) {
assertThat(response)
.hasStatusCode(Status.OK)
.entityAs(XML)
.satisfies(e -> {
assertThat(e).xpath("/user/name").asString().isEqualTo("name");
assertThat(e).xpath("/user/value").asInteger().isEqualTo(10);
});
}
Add the following dependency:
<dependency>
<groupId>com.github.attiand</groupId>
<artifactId>assertj-jaxrs-xml</artifactId>
<version>${version}</version>
<scope>test</scope>
</dependency>
There is a JUnit 5 extension that provides a jax-rs Client
parameter resolver that holds a single instance until all tests are executed.
@ExtendWith(AssertjJaxrsExtension.class)
class MyTest {
@Test
void shouldAssertStatusCodeAsInteger(Client client) {
WebTarget target = client.target("http://localhost:8081");
try (Response response = target.path("/resource").request().get()) {
assertThat(response).hasStatusCode(200);
}
}
}
Add the following dependency:
<dependency>
<groupId>com.github.attiand</groupId>
<artifactId>assertj-jaxrs-junit-jupiter</artifactId>
<version>${version}</version>
<scope>test</scope>
</dependency>
Note that the @QuarkusTest
extension does not work well with a WebTarget
parameter resolver. You can however run Quarkus tests like this.
@QuarkusTest
class GreetingResourceTest {
@TestHTTPResource
String uri;
private static final Client client = ClientBuilder.newClient();
@AfterAll
static void afterAll() {
client.close();
}
@Test
void testHelloEndpointJaxrs() {
WebTarget target = client.target(uri);
try (Response response = target.path("/hello-resteasy").request().get()) {
assertThat(response)
.hasStatusCode(200)
.entityAsText()
.isEqualTo("Hello RESTEasy");
}
}
}
1.0.0
- First version
2.0.0
- Added XPath, Json braking change path -> pathValue
2.0.1
- Added maven wrapper, javadoc builds on java 17
3.0.0
- Use jaxrs 3.0 & jsonp 2.0 (jakarta name space)