Wiremock :How to read the stub from a file in wiremock?

Viewed 6072

Currently I am setting up request and response in my stub through my java code like below.

wireMockRule.stubFor(WireMock.get(WireMock.urlEqualTo("/abc/xyz"))
                .willReturn(WireMock.aResponse()
                        .withBody("Hey")
                        .withStatus(HttpStatus.OK.value())
                        .withHeader("Content-Type",
                                "application/text")));

I wish to know if there is a possibility that I define all the stub configuration in the json file like below and just read it in my java code.

{
  "request": {
    "method": "GET",
    "url": "/abc/xyz"
  },
  "response": {
    "status": 200,
    "jsonBody": {"Hey"},
    "headers": {
      "Content-Type": "application/test"
    }
  }
}

I read about mapping folder over many post. I am not sure what that is and how do I get it and how can it help in my use case?

Also, I read about body file name but that just helps with the response body while I wish to find a solution which helps with the whole stub from a file.

A working java example would be really helpful.

1 Answers

Stubs file (src/test/resources):

stubs file in src/test/resources

Content file (stub.json):

content stubs.json file

Related