Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/attiand/assertj-jaxrs

Provides assertj assertions to jaxrs responces
https://github.com/attiand/assertj-jaxrs

assertj assertj-jaxrs jaxrs quarkus

Last synced: 18 days ago
JSON representation

Provides assertj assertions to jaxrs responces

Awesome Lists containing this project

README

        

# assertj-jaxrs

Lightweight library that provides AssertJ custom assertions to JAX-RS responses.

Example:
```java
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:
* [Status code](assertj-jaxrs-core/src/test/java/com/github/attiand/assertj/jaxrs/StatusCodeIT.java)
* [Headers](assertj-jaxrs-core/src/test/java/com/github/attiand/assertj/jaxrs/HeaderIT.java)
* [Cookies](assertj-jaxrs-core/src/test/java/com/github/attiand/assertj/jaxrs/CookieIT.java)
* [Entity](assertj-jaxrs-core/src/test/java/com/github/attiand/assertj/jaxrs/EntityIT.java)
* [MediaType](assertj-jaxrs-core/src/test/java/com/github/attiand/assertj/jaxrs/MediaTypeIT.java)
* [Date](assertj-jaxrs-core/src/test/java/com/github/attiand/assertj/jaxrs/DateIT.java)
* [Link](assertj-jaxrs-core/src/test/java/com/github/attiand/assertj/jaxrs/LinkIT.java)
* [Location](assertj-jaxrs-core/src/test/java/com/github/attiand/assertj/jaxrs/LocationIT.java)
* [Language](assertj-jaxrs-core/src/test/java/com/github/attiand/assertj/jaxrs/LanguageIT.java)

Add the following dependency :
```xml

com.github.attiand
assertj-jaxrs-core
${version}
test

```
You also need a jaxrs client implementation, for example:

```xml

org.jboss.resteasy
resteasy-client
test

```

# Json Pointer

Simple support for asserting JSON Pointer values if you don't have the entity type available:
```java
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:
```xml

com.github.attiand
assertj-jaxrs-json
${version}
test

```
You also need a `javax.json-api` implementation, for example:
```xml

org.eclipse
yasson
test

```

# XML Xpath
```java
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:
```xml

com.github.attiand
assertj-jaxrs-xml
${version}
test

```

# JUnit Jupiter extension
There is a JUnit 5 extension that provides a jax-rs `Client` parameter resolver that holds a single instance until all tests are executed.
```java
@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:
```xml

com.github.attiand
assertj-jaxrs-junit-jupiter
${version}
test

```
## Quarkus

Note that the `@QuarkusTest` extension does not work well with a `WebTarget` parameter resolver. You can however run Quarkus tests like this.
```java
@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");
}
}
}

```
# Revision history

`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)