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
This commit is contained in:
@@ -58,6 +58,8 @@ class JsonContentAssertTests {
|
||||
|
||||
private static final String SIMPSONS = loadJson("simpsons.json");
|
||||
|
||||
private static final String NULLS = loadJson("nulls.json");
|
||||
|
||||
private static JSONComparator COMPARATOR = new DefaultComparator(JSONCompareMode.LENIENT);
|
||||
|
||||
@TempDir
|
||||
@@ -839,6 +841,24 @@ class JsonContentAssertTests {
|
||||
assertThat(forJson(SOURCE)).isNotEqualToJson(createResource(DIFFERENT), COMPARATOR);
|
||||
}
|
||||
|
||||
@Test
|
||||
void hasJsonPathForPresentAndNotNull() {
|
||||
assertThat(forJson(NULLS)).hasJsonPath("valuename");
|
||||
}
|
||||
|
||||
@Test
|
||||
void hasJsonPathForPresentAndNull() {
|
||||
assertThat(forJson(NULLS)).hasJsonPath("nullname");
|
||||
}
|
||||
|
||||
@Test
|
||||
void hasJsonPathForNotPresent() {
|
||||
String expression = "missing";
|
||||
assertThatExceptionOfType(AssertionError.class)
|
||||
.isThrownBy(() -> 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");
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"valuename" : "spring",
|
||||
"nullname" : null
|
||||
}
|
||||
Reference in New Issue
Block a user