3

Am new to wiremock, am using wiremock in my spring boot integration test for mocking one of the external http call.

My json request body contains two dynamic properties, all other fields am able to set and stub in the request only two field values am not able to set as they are dynamic values.

As these are dynamic in nature, I mean value of the property is random-auto-genarated values. Is there a way to ignore these properties and remaining all properties are anyhow matching.

I know that ignore is available for extra fields and array order

withRequestBody(equalToJson(requestJson, true, true))

true ignoreArrayOrder

true ignoreExtraField

I want to know similar to above, ignore feature to specific property (user can mention the property name so that property it should ignore) is available?

I have a following json request body that I need to stub in wire mock post so am using

requestJson

{
   "property_one" : "Anand Sweets",
   "property_two" : "Guru Sweets",
   "property_three" : "Kranthi Sweets",
   "property_four" : auto-generated-number,  // dynamic number from code
   "property_five" : null,
   "propertysix" : " "GST128093",
   "property_seven" : auto-generated-number, // dynamic number from code
   "property_eight" : 13890139,
   "property_nine" : 1290.90,
   "property_ten" : "X239GDIJD9090"

}

Stubbing

stubFor(post(urlEqualTo(testUrl))
            .withHeader(CUSTKEY, equalTo(CUST_VALUE))
            .withHeader(TOKENKEY, equalTo(TOKEN_VALUE))
            .withHeader(MATCHKEY, equalTo(MATCH_VALUE))
            .withRequestBody(equalToJson(requestJson, true, true))
            .willReturn(aResponse()
                    .withStatus(200)
                    .withHeader("Content-Type", "application/json")
                    .withBody(responseJson)
            )
    );

responseJson

{
     "Success": true,
     "message": "request processed successfully",
     "success_code": 9000,
     "external": "AKALDJKAD138948934" 
}

Here in the request json, I have around almost 60 to 70 properties, except for property_four and property_seven remaining all fields match is happening properly.

I wanted to know how to ignore these two fields with equaltoJson(requestJson) in wiremock, as these field values are dynamic in nature, I wanted to skip these properties from matching.

Any one have any Idea how to skip or ignore these properties Or any other good solution to this problem.

I thought of using withRequestBody(matchingJsonPath(PATH)) but with huge and complex request,

I felt, that is not the right solution.

My request body is so complex, I mean many fields, just for example, I mentioned only ten properties with example properties and values.

Thanks in advance, as I said earlier am new to wiremock.

Dilan
  • 2,610
  • 7
  • 23
  • 33
Rajendra
  • 33
  • 1
  • 1
  • 3
  • Hey Rajendra, I've answered your question, but I think I want to challenge you on why you have to match every single field in the request body. Does a slight change in one field trigger a substantially different response? Could some of these requests be combined and have the same response? Could a response transformer be used to generate the response based on the values in the request body? Could a unique request be identified with matching substantially fewer fields? – agoff Apr 21 '20 at 20:52

1 Answers1

3

Assuming you are using WireMock version 2.26.3, you can use placeholders for any JSON value in the request. See the request matching documentation for more information (probably best to search for "placeholders").

{
    "property_one" : "Anand Sweets",
    "property_two" : "Guru Sweets",
    "property_three" : "Kranthi Sweets",
    "property_four" : "${json-unit.any-number}",  // dynamic number from code
    "property_five" : null,
    "propertysix" : " "GST128093",
    "property_seven" : "${json-unit.any-number}", // dynamic number from code
    "property_eight" : 13890139,
    "property_nine" : 1290.90,
    "property_ten" : "X239GDIJD9090"
}

For versions of WireMock prior to 2.26.3, the JSON matching isn't quite as robust, and I'd definitely just recommend upgrading.

agoff
  • 5,818
  • 1
  • 7
  • 20
  • 1
    Helpful information, I have used jsonPathMatching option and it worked. As it was a lengthy process, but worked perfectly. Later I checked the documentation. It really nice one, got good knowledge on how to use place-holders. Thanks ago – Rajendra May 11 '20 at 15:25
  • How do you "enable placeholders" in wiremock-standalone-2.27.2.jar ? (just to be clear: it does not work) – Alecz May 05 '21 at 22:05
  • @alecz are you using the `jre-8` standalone? There's a note on the `Request Matching` documentation that placeholders will only work with the jre-8 variant. You can download that version of the jar at the bottom of this page: http://wiremock.org/docs/download-and-installation/ – agoff May 06 '21 at 14:22
  • @agoff, thanks for pointing that out, I was using http://wiremock.org/docs/running-standalone/ as reference which points to `wiremock-standalone-2.27.2.jar` right at the top, not `wiremock-jre8-standalone-2.27.2.jar` which is linked at the bottom of the page you referenced. – Alecz May 07 '21 at 17:29
  • 1
    Yep! No problem -- I noticed that when I was trying to link you to the documentation as well, and have raised an issue in the WireMock github: https://github.com/tomakehurst/wiremock/issues/1479 – agoff May 07 '21 at 18:12