lukas-krecan / JsonUnit

Compare JSON in your Unit Tests
Apache License 2.0
871 stars 114 forks source link

Consider JsonComparator implementation for Spring Framework 6.2.0 #767

Closed commonquail closed 1 week ago

commonquail commented 1 month ago

This is to let JsonUnit know that Spring Framework 6.2.0 is expected to include an o.s.t.w.s.r.ContentResultMatchers::json overload that takes an o.s.t.j.JsonComparator, a BiFunction<@Nullable String, @Nullable String, JsonComparison>, that evaluates a comparison of actual and expected JSON values. JsonUnit may wish to consider this extension mechanism as a complement to its existing Spring Framework integrations.

It looks to me like no official public API of JsonUnit facilitates an ergonomic implementation of JsonComparator but that perhaps the internal Diff would be the implementation to base an extension on. For now, the following is the plainest implementation I can come up with:

        MockMvcResultMatchers.content().json(read("/foo.json"), (expectedJson, actualJson) -> {
            JsonAssertions.assertThatJson(actualJson).when(Option.IGNORING_EXTRA_FIELDS).isEqualTo(expectedJson);
            return JsonComparison.match();
        });

See also

lukas-krecan commented 1 month ago

Hi, thanks. I will look into that.

lukas-krecan commented 1 month ago

https://docs.spring.io/spring-framework/docs/6.2.0-SNAPSHOT/javadoc-api/org/springframework/test/json/JsonComparator.html

lukas-krecan commented 1 month ago

Basic support is here https://github.com/lukas-krecan/JsonUnit/pull/772/files

commonquail commented 1 month ago

That's quite elegant

lukas-krecan commented 1 week ago

It will be part of 4.x release once Spring 6.2 is out