diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/json/JsonContentAssert.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/json/JsonContentAssert.java index c2bb307831..f43738045c 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/json/JsonContentAssert.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/json/JsonContentAssert.java @@ -24,6 +24,7 @@ import java.util.Map; import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; +import com.jayway.jsonpath.PathNotFoundException; import org.assertj.core.api.AbstractAssert; import org.assertj.core.api.AbstractBooleanAssert; import org.assertj.core.api.AbstractCharSequenceAssert; @@ -748,6 +749,21 @@ public class JsonContentAssert extends AbstractAssert type, String expectedDescription) { Object value = getValue(true); if (value == null || isIndefiniteAndEmpty()) { @@ -1080,9 +1129,7 @@ public class JsonContentAssert extends AbstractAssert assertThat(forJson(NULLS)).hasJsonPath(expression)) + .withMessageContaining("No JSON path \"" + expression + "\" found"); + } + @Test void hasJsonPathValue() { assertThat(forJson(TYPES)).hasJsonPathValue("$.str"); @@ -854,6 +874,22 @@ class JsonContentAssertTests { assertThat(forJson(TYPES)).hasJsonPathValue("$.emptyMap"); } + @Test + void hasJsonPathValueForANullValue() { + String expression = "nullname"; + assertThatExceptionOfType(AssertionError.class) + .isThrownBy(() -> assertThat(forJson(NULLS)).hasJsonPathValue(expression)) + .withMessageContaining("No value at JSON path \"" + expression + "\""); + } + + @Test + void hasJsonPathValueForMissingValue() { + String expression = "missing"; + assertThatExceptionOfType(AssertionError.class) + .isThrownBy(() -> assertThat(forJson(NULLS)).hasJsonPathValue(expression)) + .withMessageContaining("No value at JSON path \"" + expression + "\""); + } + @Test void hasJsonPathValueForIndefinitePathWithResults() { assertThat(forJson(SIMPSONS)).hasJsonPathValue("$.familyMembers[?(@.name == 'Bart')]"); @@ -867,6 +903,27 @@ class JsonContentAssertTests { .withMessageContaining("No value at JSON path \"" + expression + "\""); } + @Test + void doesNotHaveJsonPathForMissing() { + assertThat(forJson(NULLS)).doesNotHaveJsonPath("missing"); + } + + @Test + void doesNotHaveJsonPathForNull() { + String expression = "nullname"; + assertThatExceptionOfType(AssertionError.class) + .isThrownBy(() -> assertThat(forJson(NULLS)).doesNotHaveJsonPath(expression)) + .withMessageContaining("Expecting no JSON path \"" + expression + "\""); + } + + @Test + void doesNotHaveJsonPathForPresent() { + String expression = "valuename"; + assertThatExceptionOfType(AssertionError.class) + .isThrownBy(() -> assertThat(forJson(NULLS)).doesNotHaveJsonPath(expression)) + .withMessageContaining("Expecting no JSON path \"" + expression + "\""); + } + @Test void doesNotHaveJsonPathValue() { assertThat(forJson(TYPES)).doesNotHaveJsonPathValue("$.bogus"); @@ -902,6 +959,11 @@ class JsonContentAssertTests { assertThat(forJson(SIMPSONS)).doesNotHaveJsonPathValue("$.familyMembers[?(@.name == 'Dilbert')]"); } + @Test + void doesNotHaveJsonPathValueForNull() { + assertThat(forJson(NULLS)).doesNotHaveJsonPathValue("nullname"); + } + @Test void hasEmptyJsonPathValueForAnEmptyString() { assertThat(forJson(TYPES)).hasEmptyJsonPathValue("$.emptyString"); diff --git a/spring-boot-project/spring-boot-test/src/test/resources/org/springframework/boot/test/json/nulls.json b/spring-boot-project/spring-boot-test/src/test/resources/org/springframework/boot/test/json/nulls.json new file mode 100644 index 0000000000..d3dc3310b4 --- /dev/null +++ b/spring-boot-project/spring-boot-test/src/test/resources/org/springframework/boot/test/json/nulls.json @@ -0,0 +1,4 @@ +{ + "valuename" : "spring", + "nullname" : null +}