3

I am using wiremock for mocking rest service for Junit in Spring boot application. My problem is, i am not able to match multiple match patterns.

Junit.java

StringValuePattern pattern = WireMock.matching(".*");
givenThat(post(urlEqualTo("/softwares"))
    .withHeader("Content-Type", equalTo("application/json"))
    //TODO: Matching exact JSON body
    .withRequestBody(pattern)
    .willReturn(aResponse()
    .withStatus(201)
    .withHeader("Content-Type", "application/json")
    .withBody("{\"userId\":\"ID009\"}")));

Above code snippets, i am using (.*) for match. It works fine. But, i need to match exact things which is in deployed JSON in wiremock's mapping folder

"matchesJsonPath" : "$.name",
"matchesJsonPath" : "$.protocol",
"matchesJsonPath" : "$.website",
"matchesJsonPath" : "$.website.user",
"matchesJsonPath" : "$.website.urlpath",
"matchesJsonPath" : "$.website.userlist",
"matchesJsonPath" : "$.website.resources.friendsList"

Sample Json-which is deployed in wiremock's mapping folder

{
  "request" : {
    "urlPath" : "/softwares",
    "method" : "POST",
    "basicAuthCredentials" : {"username" : "username", "password" : "password"},
    "headers" : {
      "Content-Type" : {
        "equalTo" : "application/json"
      }
    },
    "bodyPatterns" : [ {
      "matchesJsonPath" : "$.name",
      "matchesJsonPath" : "$.protocol",
      "matchesJsonPath" : "$.website",
      "matchesJsonPath" : "$.website.user",
      "matchesJsonPath" : "$.website.urlpath",
      "matchesJsonPath" : "$.website.userlist",
      "matchesJsonPath" : "$.website.resources.friendsList"
    } ]
  },

  "response" : {
    "status" : 201,
    "headers" : { "Content-Type" : "application/json"},
    "body": "{ \"userId\" : \"ID009\"}"
    }
}

1 Answers1

0

You should try extending wiremock.

Whenever a request comes, this will forward the request to a method, in that method you can get the request string and then perform all the validation and return/reroute/reject the request. It is a very powerful functionality.

here are some more examples http://one.wiremock.org/extending-wiremock.html

best wishes
  • 5,789
  • 1
  • 34
  • 59