Closed cchangenot closed 2 years ago
This is because the expectation you have created has an empty json object as the body because the JSON serialisation of Map skips null fields. If you want to match a null value in json you need to do the following:
RequestDefinition requestDefinition = request()
.withMethod("POST")
.withPath("/some/path")
.withBody(json("{\n" +
" \"myNullProperty\" : null\n" +
" }", MatchType.STRICT));
The following complete example works:
// Given
MockServerClient mockServerClient = ClientAndServer.startClientAndServer(1080);
RequestDefinition requestDefinition = request()
.withMethod("POST")
.withPath("/some/path")
.withBody(json("{\n" +
" \"myNullProperty\" : null\n" +
" }", MatchType.STRICT));
mockServerClient.when(requestDefinition).respond(response().withStatusCode(204));
RequestBody body = RequestBody.create(MediaType.parse("application/json"), "{\"myNullProperty\":null}");
Request request = new Request.Builder().post(body)
.url("http://localhost:1080/some/path")
.build();
//When
new OkHttpClient().newCall(request).execute();
// Then
mockServerClient.verify(requestDefinition);
Where as your example doesn't, as follows:
// Given
MockServerClient mockServerClient = ClientAndServer.startClientAndServer(1080);
Map<String, String> map = new HashMap<>();
map.put("myNullProperty", null);
RequestDefinition requestDefinition = request()
.withMethod("POST")
.withPath("/some/path")
.withBody(json(map, MatchType.STRICT));
mockServerClient.when(requestDefinition).respond(response().withStatusCode(204));
RequestBody body = RequestBody.create(MediaType.parse("application/json"), "{\"myNullProperty\":null}");
Request request = new Request.Builder().post(body)
.url("http://localhost:1080/some/path")
.build();
//When
new OkHttpClient().newCall(request).execute();
// Then
mockServerClient.verify(requestDefinition);
Because as show in the log you provided the expectation has an empty body:
mockserver_1 | 2021-05-10 14:38:58 5.11.1 INFO 1080 creating expectation:
mockserver_1 | {
mockserver_1 | "id" : "60952d6a-ed65-468d-b3dd-60e653d147c6",
mockserver_1 | "priority" : 0,
mockserver_1 | "httpRequest" : {
mockserver_1 | "method" : "POST",
mockserver_1 | "path" : "/some/path",
mockserver_1 | "body" : {
mockserver_1 | "type" : "JSON",
mockserver_1 | "json" : { },
mockserver_1 | "matchType" : "STRICT"
mockserver_1 | }
mockserver_1 | },
mockserver_1 | "times" : {
mockserver_1 | "unlimited" : true
mockserver_1 | },
mockserver_1 | "timeToLive" : {
mockserver_1 | "unlimited" : true
mockserver_1 | },
mockserver_1 | "httpResponse" : {
mockserver_1 | "statusCode" : 204
mockserver_1 | }
mockserver_1 | }
mockserver_1 | with id:
mockserver_1 | 60952d6a-ed65-468d-b3dd-60e653d147c6
Describe the issue When using strict mode and json body, no match found for a request with null values in some json fields.
What you are trying to do I want to be sure that a request has been sent with a json body that contains null values for some specific fields. If i don't use the strict mode, the json fields with null values are ignored and i can't be sure that that the field was null for real.
MockServer version 5.11.1
To Reproduce Here is a small failing test case :
RequestDefinition requestDefinition = request().withMethod("POST") .withPath("/some/path") .withBody(json(singletonMap("myNullProperty", null), MatchType.STRICT));
mockServerClient.when(requestDefinition).respond(response().withStatusCode(204));
Actual :
Expected behaviour The strict expectations handle null values.
I think that the problem is that null values are removed from the expectation when using strict mode. I suspect these are the problematic lines (ObjectMapperFactory#buildObjectMapper):
MockServer Log