Wildcard Matches With Wiremock


Wiremock is a great tool to use to mock out external APIs and services and you can easily test what payload you may be sending when your payload may not be the return value of any testable method. We can do it with something like this:

WireMockServer wm = ... // instantiate and intialize elsewhere
String expected = """
{
"id": 1,
"timestamp": "2022-02-015T00:00:00.000"
}
"""
wm.verify(1, postRequestedFor(urlMatching(UPDATE_ENDPOINT))
.withRequestBody(equalToJson(expected)));

 

 

This works nicely, except in cases where timestamp might be set dynamically, such as with a call to now().

In this case Wiremock provides a wildcard placeholder which will allow us to match any value here:

 

String expected = """
{
"id": 1,
"timestamp": "${json-unit.any-string}"
}
"""

With the wildcard ${json-unit.any-string} this test may now pass when dynamic values for the timestamp are set.

Leave a Reply

Your email address will not be published. Required fields are marked *