wiremock-jaxrs

Wiremock with JAXRS support. Enables creating stubs from JAXRS annotated resources.

License

License

Categories

Categories

Wire Data Data Structures
GroupId

GroupId

se.bjurr.wiremock
ArtifactId

ArtifactId

wiremock-jaxrs
Last Version

Last Version

0.2
Release Date

Release Date

Type

Type

jar
Description

Description

wiremock-jaxrs
Wiremock with JAXRS support. Enables creating stubs from JAXRS annotated resources.
Project URL

Project URL

https://github.com/tomasbjerre/wiremock-jaxrs
Source Code Management

Source Code Management

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

Download wiremock-jaxrs

How to add to project

<!-- https://jarcasting.com/artifacts/se.bjurr.wiremock/wiremock-jaxrs/ -->
<dependency>
    <groupId>se.bjurr.wiremock</groupId>
    <artifactId>wiremock-jaxrs</artifactId>
    <version>0.2</version>
</dependency>
// https://jarcasting.com/artifacts/se.bjurr.wiremock/wiremock-jaxrs/
implementation 'se.bjurr.wiremock:wiremock-jaxrs:0.2'
// https://jarcasting.com/artifacts/se.bjurr.wiremock/wiremock-jaxrs/
implementation ("se.bjurr.wiremock:wiremock-jaxrs:0.2")
'se.bjurr.wiremock:wiremock-jaxrs:jar:0.2'
<dependency org="se.bjurr.wiremock" name="wiremock-jaxrs" rev="0.2">
  <artifact name="wiremock-jaxrs" type="jar" />
</dependency>
@Grapes(
@Grab(group='se.bjurr.wiremock', module='wiremock-jaxrs', version='0.2')
)
libraryDependencies += "se.bjurr.wiremock" % "wiremock-jaxrs" % "0.2"
[se.bjurr.wiremock/wiremock-jaxrs "0.2"]

Dependencies

compile (2)

Group / Artifact Type Version
com.github.tomakehurst : wiremock jar 2.23.2
javax : javaee-api jar 8.0

Project Modules

There are no modules declared in this project.

Wiremock JAX-RS

Build Status Maven Central Bintray

Wiremock with JAX-RS support. Enables creation of stubs from JAX-RS annotated resources. It:

  • Automates configuration of stubs 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 by gathering information from the JAX-RS annotations on the given resource.

Usage

It extends, and works just like, Wiremock by adding a new factory method:

WiremockJaxrs.invocation(Class<T> resource, ResourceInvocation<T> invocation)

That is used like:

import static com.github.tomakehurst.wiremock.client.WireMockJaxrs.invocation;
...
invocation(ItemResouce.class, (r) -> r.whateverMethod(anyParameterValue))

See also:

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

Example

When invoked like this:

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMockJaxrs.invocation;
...
final List<ItemDTO> 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):

{
  "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:

@Path("/")
public interface ItemResouce {

  @Path("/list")
  @GET
  @Produces(MediaType.APPLICATION_JSON)
  public List<ItemDTO> 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:

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.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):

{
  "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!

Versions

Version
0.2
0.1