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 1f5cda8aa4e..f2992f1418c 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 @@ -24,8 +24,9 @@ import java.util.function.Consumer; import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.PathNotFoundException; -import org.assertj.core.api.AbstractStringAssert; +import org.assertj.core.api.AbstractObjectAssert; import org.assertj.core.api.AssertProvider; +import org.assertj.core.api.Assertions; import org.assertj.core.error.BasicErrorMessageFactory; import org.assertj.core.internal.Failures; @@ -61,7 +62,7 @@ import org.springframework.util.Assert; * @param the type of assertions */ public abstract class AbstractJsonContentAssert> - extends AbstractStringAssert { + extends AbstractObjectAssert { private static final Failures failures = Failures.instance(); @@ -79,16 +80,12 @@ public abstract class AbstractJsonContentAssertPath can be converted to a value object using the given - * {@linkplain GenericHttpMessageConverter JSON message converter}. - * @param json the JSON document to assert - * @param jsonMessageConverter the converter to use + * @param actual the JSON document to assert * @param selfType the implementation type of this assert */ - protected AbstractJsonContentAssert(@Nullable String json, - @Nullable GenericHttpMessageConverter jsonMessageConverter, Class selfType) { - super(json, selfType); - this.jsonMessageConverter = jsonMessageConverter; + protected AbstractJsonContentAssert(@Nullable JsonContent actual, Class selfType) { + super(actual, selfType); + this.jsonMessageConverter = (actual != null ? actual.getJsonMessageConverter() : null); this.jsonLoader = new JsonLoader(null, null); as("JSON content"); } @@ -141,6 +138,19 @@ public abstract class AbstractJsonContentAssert { private final String json; @Nullable - private final Class resourceLoadClass; + private final GenericHttpMessageConverter jsonMessageConverter; + + + /** + * Create a new {@code JsonContent} instance with the message converter to + * use to deserialize content. + * @param json the actual JSON content + * @param jsonMessageConverter the message converter to use + */ + public JsonContent(String json, @Nullable GenericHttpMessageConverter jsonMessageConverter) { + Assert.notNull(json, "JSON must not be null"); + this.json = json; + this.jsonMessageConverter = jsonMessageConverter; + } /** * Create a new {@code JsonContent} instance. * @param json the actual JSON content - * @param resourceLoadClass the source class used to load resources */ - JsonContent(String json, @Nullable Class resourceLoadClass) { - Assert.notNull(json, "JSON must not be null"); - this.json = json; - this.resourceLoadClass = resourceLoadClass; + public JsonContent(String json) { + this(json, null); } - /** * Use AssertJ's {@link org.assertj.core.api.Assertions#assertThat assertThat} * instead. */ @Override public JsonContentAssert assertThat() { - return new JsonContentAssert(this.json, null).withResourceLoadClass(this.resourceLoadClass); + return new JsonContentAssert(this); } /** * Return the actual JSON content string. - * @return the JSON content */ public String getJson() { return this.json; } + /** + * Return the message converter to use to deserialize content. + */ + @Nullable + GenericHttpMessageConverter getJsonMessageConverter() { + return this.jsonMessageConverter; + } + @Override public String toString() { return "JsonContent " + this.json; diff --git a/spring-test/src/main/java/org/springframework/test/json/JsonContentAssert.java b/spring-test/src/main/java/org/springframework/test/json/JsonContentAssert.java index de4a35f80f9..9728a762496 100644 --- a/spring-test/src/main/java/org/springframework/test/json/JsonContentAssert.java +++ b/spring-test/src/main/java/org/springframework/test/json/JsonContentAssert.java @@ -16,7 +16,6 @@ package org.springframework.test.json; -import org.springframework.http.converter.GenericHttpMessageConverter; import org.springframework.lang.Nullable; /** @@ -29,13 +28,10 @@ public class JsonContentAssert extends AbstractJsonContentAssertPath can be converted to a value object using the given - * {@linkplain GenericHttpMessageConverter JSON message converter}. * @param json the JSON document to assert - * @param jsonMessageConverter the converter to use */ - public JsonContentAssert(@Nullable String json, @Nullable GenericHttpMessageConverter jsonMessageConverter) { - super(json, jsonMessageConverter, JsonContentAssert.class); + public JsonContentAssert(@Nullable JsonContent json) { + super(json, JsonContentAssert.class); } } diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/assertj/AbstractMockHttpServletResponseAssert.java b/spring-test/src/main/java/org/springframework/test/web/servlet/assertj/AbstractMockHttpServletResponseAssert.java index 9800b2865db..1bbfd051d94 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/assertj/AbstractMockHttpServletResponseAssert.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/assertj/AbstractMockHttpServletResponseAssert.java @@ -27,6 +27,7 @@ import org.springframework.http.converter.GenericHttpMessageConverter; import org.springframework.lang.Nullable; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.test.json.AbstractJsonContentAssert; +import org.springframework.test.json.JsonContent; import org.springframework.test.json.JsonContentAssert; import org.springframework.test.web.UriAssert; @@ -92,7 +93,7 @@ public abstract class AbstractMockHttpServletResponseAssert */ public AbstractJsonContentAssert bodyJson() { - return new JsonContentAssert(readBody(), this.jsonMessageConverter); + return new JsonContentAssert(new JsonContent(readBody(), this.jsonMessageConverter)); } private String readBody() { diff --git a/spring-test/src/test/java/org/springframework/test/json/AbstractJsonContentAssertTests.java b/spring-test/src/test/java/org/springframework/test/json/AbstractJsonContentAssertTests.java index 37568a6d585..0c350c41357 100644 --- a/spring-test/src/test/java/org/springframework/test/json/AbstractJsonContentAssertTests.java +++ b/spring-test/src/test/java/org/springframework/test/json/AbstractJsonContentAssertTests.java @@ -92,6 +92,14 @@ class AbstractJsonContentAssertTests { assertThat(forJson(null)).isNull(); } + @Test + void satisfiesAllowFurtherAssertions() { + assertThat(forJson(SIMPSONS)).satisfies(content -> { + assertThat(content).extractingPath("$.familyMembers[0].name").isEqualTo("Homer"); + assertThat(content).extractingPath("$.familyMembers[1].name").isEqualTo("Marge"); + }); + } + @Nested class HasPathTests { @@ -831,7 +839,7 @@ class AbstractJsonContentAssertTests { private static class TestJsonContentAssert extends AbstractJsonContentAssert { public TestJsonContentAssert(@Nullable String json, @Nullable GenericHttpMessageConverter jsonMessageConverter) { - super(json, jsonMessageConverter, TestJsonContentAssert.class); + super((json != null ? new JsonContent(json, jsonMessageConverter) : null), TestJsonContentAssert.class); } } diff --git a/spring-test/src/test/java/org/springframework/test/json/JsonContentTests.java b/spring-test/src/test/java/org/springframework/test/json/JsonContentTests.java index 6e4131c46f6..b87e8849b05 100644 --- a/spring-test/src/test/java/org/springframework/test/json/JsonContentTests.java +++ b/spring-test/src/test/java/org/springframework/test/json/JsonContentTests.java @@ -18,13 +18,17 @@ package org.springframework.test.json; import org.junit.jupiter.api.Test; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; + import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.mockito.Mockito.mock; /** * Tests for {@link JsonContent}. * * @author Phillip Webb + * @author Stephane Nicoll */ class JsonContentTests { @@ -34,27 +38,33 @@ class JsonContentTests { void createWhenJsonIsNullShouldThrowException() { assertThatIllegalArgumentException() .isThrownBy( - () -> new JsonContent(null, null)) + () -> new JsonContent(null)) .withMessageContaining("JSON must not be null"); } @Test - @SuppressWarnings("deprecation") void assertThatShouldReturnJsonContentAssert() { - JsonContent content = new JsonContent(JSON, getClass()); + JsonContent content = new JsonContent(JSON); assertThat(content.assertThat()).isInstanceOf(JsonContentAssert.class); } @Test void getJsonShouldReturnJson() { - JsonContent content = new JsonContent(JSON, getClass()); + JsonContent content = new JsonContent(JSON); assertThat(content.getJson()).isEqualTo(JSON); } @Test void toStringShouldReturnString() { - JsonContent content = new JsonContent(JSON, getClass()); + JsonContent content = new JsonContent(JSON); assertThat(content.toString()).isEqualTo("JsonContent " + JSON); } + @Test + void getJsonMessageConverterShouldReturnConverter() { + MappingJackson2HttpMessageConverter converter = mock(MappingJackson2HttpMessageConverter.class); + JsonContent content = new JsonContent(JSON, converter); + assertThat(content.getJsonMessageConverter()).isSameAs(converter); + } + }