wiremock post request json body property value is dynamic generated value how to stub

穿精又带淫゛_ 提交于 2021-02-08 10:45:18

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/61289508/wiremock-post-request-json-body-property-value-is-dynamic-generated-value-how-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!