I am trying to verify that the POST call made to downstream service has the correct body/value, but getting a null exception.
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.exactly;
import static com.github.tomakehurst.wiremock.client.WireMock.matchingJsonPath;
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
@Test
void test() throws Exception {
mockMvc.perform(
post(...)
.andExpect(status().isOk());
await().atMost(Duration.ofSeconds(5)).untilAsserted(() -> verify(exactly(1), postRequestedFor(urlPathMatching("my-url-path"))
.withRequestBody(matchingJsonPath("$.field1", equalTo("1")))));
}
Fails with this warning and exception
com.github.tomakehurst.wiremock.matching.PathPattern$SubExpressionException: Warning: JSON path expression '$.accountId' failed to match document '' because of error 'json string can not be null or empty'
Caused by: java.lang.IllegalArgumentException: json string can not be null or empty
at wiremock.com.jayway.jsonpath.internal.Utils.notEmpty(Utils.java:401)
at wiremock.com.jayway.jsonpath.internal.ParseContextImpl.parse(ParseContextImpl.java:36)
at wiremock.com.jayway.jsonpath.JsonPath.read(JsonPath.java:550)
at com.github.tomakehurst.wiremock.matching.MatchesJsonPathPattern.getExpressionResult(MatchesJsonPathPattern.java:122)
... 18 more
Instead, if I use expression mapping it works fine.
.withRequestBody(matchingJsonPath("$.[?(@.field1 == 1)]"))));
Whats the correct way to use StringValuePattern?
My json body looks like this:
{
"field1": "1"
}