From 0d92af7d55697e0b2a4ae963eaeb702b193c5c77 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 23 Jul 2019 15:19:32 +0100 Subject: [PATCH] Add hasJsonPath and doesNotHaveJsonPath asserts Extend `JsonContentAssert` with `hasJsonPath` and `doesNotHaveJsonPath` methods which can be used to check the path regardless of the value it may or may not contain. Prior to this commit there wasn't an easy way to assert that the Jackson `@JsonInclude(JsonInclude.Include.NON_NULL)` annotation was applied since `assertDoesNotHavePathValue` would pass for both `{"name" : null}` and `{}`. Closes gh-17608 --- .../boot/test/json/JsonContentAssert.java | 58 ++++++++++++++++- .../test/json/JsonContentAssertTests.java | 62 +++++++++++++++++++ .../springframework/boot/test/json/nulls.json | 4 ++ 3 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 spring-boot-project/spring-boot-test/src/test/resources/org/springframework/boot/test/json/nulls.json 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 +}