Skip to content

Commit 3bfcfaa

Browse files
committed
Improve null-safety of JsonContentAssert
See gh-46926
1 parent 96afc3f commit 3bfcfaa

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

core/spring-boot-test/src/main/java/org/springframework/boot/test/json/JsonContentAssert.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,9 +1000,11 @@ private JSONCompareResult compare(@Nullable CharSequence expectedJson, JSONCompa
10001000
if (this.actual == null) {
10011001
return compareForNull(expectedJson);
10021002
}
1003+
if (expectedJson == null) {
1004+
return fail("Expected JSON but got null");
1005+
}
10031006
try {
1004-
return JSONCompare.compareJSON((expectedJson != null) ? expectedJson.toString() : null,
1005-
this.actual.toString(), compareMode);
1007+
return JSONCompare.compareJSON(expectedJson.toString(), this.actual.toString(), compareMode);
10061008
}
10071009
catch (Exception ex) {
10081010
if (ex instanceof RuntimeException runtimeException) {
@@ -1016,9 +1018,11 @@ private JSONCompareResult compare(@Nullable CharSequence expectedJson, JSONCompa
10161018
if (this.actual == null) {
10171019
return compareForNull(expectedJson);
10181020
}
1021+
if (expectedJson == null) {
1022+
return fail("Expected JSON but got null");
1023+
}
10191024
try {
1020-
return JSONCompare.compareJSON((expectedJson != null) ? expectedJson.toString() : null,
1021-
this.actual.toString(), comparator);
1025+
return JSONCompare.compareJSON(expectedJson.toString(), this.actual.toString(), comparator);
10221026
}
10231027
catch (Exception ex) {
10241028
if (ex instanceof RuntimeException runtimeException) {
@@ -1037,6 +1041,12 @@ private JSONCompareResult compareForNull(@Nullable CharSequence expectedJson) {
10371041
return result;
10381042
}
10391043

1044+
private JSONCompareResult fail(String message) {
1045+
JSONCompareResult result = new JSONCompareResult();
1046+
result.fail(message);
1047+
return result;
1048+
}
1049+
10401050
private JsonContentAssert assertNotFailed(JSONCompareResult result) {
10411051
if (result.failed()) {
10421052
failWithMessage("JSON Comparison failure: %s", result.getMessage());

0 commit comments

Comments
 (0)