Allow JSON content assertions to be nested
Previously, AbstractJsonContentAssert worked on a raw String, which made standard AssertJ nested calls, such as satisfies, to return an assert on the raw string, rather than one with JSON support. This commit rework AbstractJsonContentAssert so that it no longer extend from AbstractStringAssert. This makes the list of methods more focused on JSON assertions, and allow standard operations to provide the right assert object. Closes gh-32894
This commit is contained in:
@@ -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<TestJsonContentAssert> {
|
||||
|
||||
public TestJsonContentAssert(@Nullable String json, @Nullable GenericHttpMessageConverter<Object> jsonMessageConverter) {
|
||||
super(json, jsonMessageConverter, TestJsonContentAssert.class);
|
||||
super((json != null ? new JsonContent(json, jsonMessageConverter) : null), TestJsonContentAssert.class);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user