Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/wiremock/wiremock-jaxrs

Automates configuration of Wiremock stubs from JAX-RS annotated resources.
https://github.com/wiremock/wiremock-jaxrs

mock wiremock wiremock-mappings

Last synced: about 2 months ago
JSON representation

Automates configuration of Wiremock stubs from JAX-RS annotated resources.

Awesome Lists containing this project

README

        

# Wiremock JAX-RS

[![Maven Central](https://maven-badges.herokuapp.com/maven-central/se.bjurr.wiremock/wiremock-jaxrs/badge.svg)](https://maven-badges.herokuapp.com/maven-central/se.bjurr.wiremock/wiremock-jaxrs)

[Wiremock](http://wiremock.org/) with JAX-RS support. Enables creation of stubs from JAX-RS annotated resources. It:

* Automates configuration of [stubs](http://wiremock.org/docs/stubbing/) for those using JAX-RS.
* Contains validation checks against JAX-RS which enables you to produce **type safe stubs**.

Given:

* JAX-RS annotated resource
* Called method
* Response (unless void)

It will create a [Wiremock stub](http://wiremock.org/docs/stubbing/) by gathering information from the JAX-RS annotations on the given resource.

## Usage

It extends, and works just like, [Wiremock](http://wiremock.org/docs/stubbing/) by adding a new factory method:

```java
WireMockJaxrs.invocation(Class resource, ResourceInvocation invocation)
```

That is used like:

```java
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.jaxrs.api.WireMockJaxrs.invocation;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
...
final StubMapping sm =
stubFor( //
invocation(ItemResouce.class, (r) -> r.whateverMethod(anyParameterValue)) //
.willReturn(aResponse().withStatus(SC_ACCEPTED), responseObject));
```

### Example

When invoked like this:

```java
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.jaxrs.api.WireMockJaxrs.invocation;
...
final List responseObject = Arrays.asList(new ItemDTO("pong"));
final StubMapping sm =
stubFor( //
invocation(ItemResouce.class, (r) -> r.getItems()) //
.willReturn(aResponse().withStatus(SC_ACCEPTED), responseObject));
```

It creates a stub (as described [here](http://wiremock.org/docs/stubbing/)):

```json
{
"id" : "d68fb4e2-48ed-40d2-bc73-0a18f54f3ece",
"request" : {
"urlPattern" : ".*/list$",
"method" : "GET",
"headers" : {
"Accept" : {
"equalTo" : "application/json"
}
}
},
"response" : {
"status" : 202,
"body" : "[{\"str\":\"pong\",\"id\":0}]",
"headers" : {
"Content-Type" : "application/json"
}
},
"uuid" : "d68fb4e2-48ed-40d2-bc73-0a18f54f3ece"
}
```

When `ItemResource` looks like:

```java
@Path("/")
public interface ItemResouce {

@Path("/list")
@GET
@Produces(MediaType.APPLICATION_JSON)
public List getItems();

@Path("/create")
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public ItemDTO post(ItemDTO item);
}
```

If the method consumes content, that content is also matched. When invoked like this:

```java
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.jaxrs.api.WireMockJaxrs.invocation;
...
final ItemDTO responseObject = new ItemDTO("the item");
responseObject.setId(123);
final ItemDTO postedItem = new ItemDTO("the item");

final StubMapping sm =
stubFor( //
invocation(ItemResouce.class, (r) -> r.post(postedItem)) //
.willReturn(aResponse().withStatus(SC_ACCEPTED), responseObject));
```

It creates a stub (as described [here](http://wiremock.org/docs/stubbing/)):

```json
{
"id" : "d68fb4e2-48ed-40d2-bc73-0a18f54f3ece",
"request" : {
"urlPattern" : ".*/create$",
"method" : "POST",
"headers" : {
"Content-Type" : {
"equalTo" : "application/json"
},
"Accept" : {
"equalTo" : "application/json"
}
},
"bodyPatterns" : [ {
"equalToJson" : "{\"str\":\"the item\",\"id\":0}",
"ignoreArrayOrder" : true,
"ignoreExtraElements" : true
} ]
},
"response" : {
"status" : 202,
"body" : "{\"str\":\"the item\",\"id\":123}",
"headers" : {
"Content-Type" : "application/json"
}
},
"uuid" : "d68fb4e2-48ed-40d2-bc73-0a18f54f3ece"
}
```

Check the test cases in this repository for more examples!