Home » Java » java – Is it possible to pass in a file as part of the request body using WireMock?-Exceptionshub

java – Is it possible to pass in a file as part of the request body using WireMock?-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am currently writing an integration test using Wiremock. At the moment I am defining the id and organization id as parameters. Instead of repeating this process for say 50 fields, is it possible to pass in a JSON file as the request instead? ie use this file as the requestBody.

stubFor(post(urlEqualTo("/v1/transaction"))
            .withRequestBody(
                    matchingJsonPath("$.data.id", containing("1")))
            .withRequestBody(matchingJsonPath("$.data.organisation_id", containing("2")))
            .willReturn(aResponse()
                    .withHeader(CONTENT_TYPE, APPLICATION_JSON_VALUE)
                    .withStatus(HttpStatus.CREATED.value())
                    .withBodyFile("create_successful_response.json")));
How to&Answers:

You might find that the placeholders feature added in 2.26.0 is a better fit for what you want here.

Placeholders allow you to use equalToJson(...) with looser matching of specific fields e.g.

{
  "data": {
    "id": "${json-unit.any-string}",
    "organisation_id": "${json-unit.regex}.*1.*}"
  }
}

More details here: http://wiremock.org/docs/request-matching/#placeholders