From d8dcd3af0b446b7bd5718201f3054eb0a0cc3d21 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 20 May 2024 11:08:20 -0700 Subject: [PATCH] Introduce JSON comparison abstraction for tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit introduces a JsonComparator abstraction, with an implementation using JSONAssert. Previously, JSONAssert was the only choice. Test APIs have been adapted to allow the new abstraction while relying on JSONAssert still for high-level methods. Closes gh-32791 Co-authored-by: Stéphane Nicoll --- .../test/json/AbstractJsonContentAssert.java | 124 +++++++----------- .../springframework/test/json/JsonAssert.java | 93 +++++++++++++ .../test/json/JsonComparator.java | 54 ++++++++ .../test/json/JsonCompareMode.java | 37 ++++++ .../test/json/JsonComparison.java | 91 +++++++++++++ .../test/util/JsonExpectationsHelper.java | 5 + .../client/match/ContentRequestMatchers.java | 43 +++++- .../reactive/server/DefaultWebTestClient.java | 18 ++- .../web/reactive/server/WebTestClient.java | 35 ++++- .../servlet/result/ContentResultMatchers.java | 50 +++++-- .../result/ContentResultMatchersDsl.kt | 14 +- .../json/AbstractJsonContentAssertTests.java | 83 +++++++++--- .../server/samples/JsonContentTests.java | 5 +- .../web/servlet/MockMvcExtensionsTests.kt | 5 +- 14 files changed, 539 insertions(+), 118 deletions(-) create mode 100644 spring-test/src/main/java/org/springframework/test/json/JsonAssert.java create mode 100644 spring-test/src/main/java/org/springframework/test/json/JsonComparator.java create mode 100644 spring-test/src/main/java/org/springframework/test/json/JsonCompareMode.java create mode 100644 spring-test/src/main/java/org/springframework/test/json/JsonComparison.java diff --git a/spring-test/src/main/java/org/springframework/test/json/AbstractJsonContentAssert.java b/spring-test/src/main/java/org/springframework/test/json/AbstractJsonContentAssert.java index 37cf2cfd3c1..1f5cda8aa4e 100644 --- a/spring-test/src/main/java/org/springframework/test/json/AbstractJsonContentAssert.java +++ b/spring-test/src/main/java/org/springframework/test/json/AbstractJsonContentAssert.java @@ -28,10 +28,6 @@ import org.assertj.core.api.AbstractStringAssert; import org.assertj.core.api.AssertProvider; import org.assertj.core.error.BasicErrorMessageFactory; import org.assertj.core.internal.Failures; -import org.skyscreamer.jsonassert.JSONCompare; -import org.skyscreamer.jsonassert.JSONCompareMode; -import org.skyscreamer.jsonassert.JSONCompareResult; -import org.skyscreamer.jsonassert.comparator.JSONComparator; import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.ClassPathResource; @@ -41,7 +37,6 @@ import org.springframework.core.io.Resource; import org.springframework.http.converter.GenericHttpMessageConverter; import org.springframework.lang.Nullable; import org.springframework.util.Assert; -import org.springframework.util.function.ThrowingBiFunction; /** * Base AssertJ {@linkplain org.assertj.core.api.Assert assertions} that can be @@ -51,8 +46,8 @@ import org.springframework.util.function.ThrowingBiFunction; * extracting a part of the document for further {@linkplain JsonPathValueAssert * assertions} on the value. * - *

Also supports comparing the JSON document against a target, using - * {@linkplain JSONCompare JSON Assert}. Resources that are loaded from + *

Also supports comparing the JSON document against a target, using a + * {@linkplain JsonComparator JSON Comparator}. Resources that are loaded from * the classpath can be relative if a {@linkplain #withResourceLoadClass(Class) * class} is provided. By default, {@code UTF-8} is used to load resources, * but this can be overridden using {@link #withCharset(Charset)}. @@ -154,9 +149,9 @@ public abstract class AbstractJsonContentAssertThe resource abstraction allows to provide several input types: *