Skip to content

Commit ff8edf8

Browse files
Camille Vienotsnicoll
authored andcommitted
Use assertJ to generate AssertionError and filter its stacktrace
Closes gh-15569
1 parent 51936e1 commit ff8edf8

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
*
4646
* @author Phillip Webb
4747
* @author Andy Wilkinson
48+
* @author Camille Vienot
4849
* @since 1.4.0
4950
*/
5051
public class JsonContentAssert extends AbstractAssert<JsonContentAssert, CharSequence> {
@@ -96,7 +97,8 @@ public JsonContentAssert isEqualTo(Object expected) {
9697
if (expected instanceof Resource) {
9798
return isEqualToJson((Resource) expected);
9899
}
99-
throw new AssertionError("Unsupport type for JSON assert " + expected.getClass());
100+
failWithMessage("Unsupport type for JSON assert {}", expected.getClass());
101+
return null;
100102
}
101103

102104
/**
@@ -432,7 +434,8 @@ public JsonContentAssert isNotEqualTo(Object expected) {
432434
if (expected instanceof Resource) {
433435
return isNotEqualToJson((Resource) expected);
434436
}
435-
throw new AssertionError("Unsupport type for JSON assert " + expected.getClass());
437+
failWithMessage("Unsupport type for JSON assert {]", expected.getClass());
438+
return null;
436439
}
437440

438441
/**
@@ -1031,14 +1034,14 @@ private JSONCompareResult compareForNull(CharSequence expectedJson) {
10311034

10321035
private JsonContentAssert assertNotFailed(JSONCompareResult result) {
10331036
if (result.failed()) {
1034-
throw new AssertionError("JSON Comparison failure: " + result.getMessage());
1037+
failWithMessage("JSON Comparison failure: {}", result.getMessage());
10351038
}
10361039
return this;
10371040
}
10381041

10391042
private JsonContentAssert assertNotPassed(JSONCompareResult result) {
10401043
if (result.passed()) {
1041-
throw new AssertionError("JSON Comparison failure: " + result.getMessage());
1044+
failWithMessage("JSON Comparison failure: {}", result.getMessage());
10421045
}
10431046
return this;
10441047
}
@@ -1064,32 +1067,32 @@ public void assertHasEmptyValue() {
10641067
if (ObjectUtils.isEmpty(getValue(false)) || isIndefiniteAndEmpty()) {
10651068
return;
10661069
}
1067-
throw new AssertionError(getExpectedValueMessage("an empty value"));
1070+
failWithMessage(getExpectedValueMessage("an empty value"));
10681071
}
10691072

10701073
public void assertDoesNotHaveEmptyValue() {
10711074
if (!ObjectUtils.isEmpty(getValue(false))) {
10721075
return;
10731076
}
1074-
throw new AssertionError(getExpectedValueMessage("a non-empty value"));
1077+
failWithMessage(getExpectedValueMessage("a non-empty value"));
10751078

10761079
}
10771080

10781081
public void assertHasValue(Class<?> type, String expectedDescription) {
10791082
Object value = getValue(true);
10801083
if (value == null || isIndefiniteAndEmpty()) {
1081-
throw new AssertionError(getNoValueMessage());
1084+
failWithMessage(getNoValueMessage());
10821085
}
10831086
if (type != null && !type.isInstance(value)) {
1084-
throw new AssertionError(getExpectedValueMessage(expectedDescription));
1087+
failWithMessage(getExpectedValueMessage(expectedDescription));
10851088
}
10861089
}
10871090

10881091
public void assertDoesNotHaveValue() {
10891092
if (getValue(false) == null || isIndefiniteAndEmpty()) {
10901093
return;
10911094
}
1092-
throw new AssertionError(getExpectedValueMessage("no value"));
1095+
failWithMessage(getExpectedValueMessage("no value"));
10931096
}
10941097

10951098
private boolean isIndefiniteAndEmpty() {
@@ -1110,10 +1113,10 @@ public Object getValue(boolean required) {
11101113
return this.jsonPath.read((json != null) ? json.toString() : null);
11111114
}
11121115
catch (Exception ex) {
1113-
if (!required) {
1114-
return null;
1116+
if (required) {
1117+
failWithMessage("{}. {}", getNoValueMessage(), ex.getMessage());
11151118
}
1116-
throw new AssertionError(getNoValueMessage() + ". " + ex.getMessage());
1119+
return null;
11171120
}
11181121
}
11191122

0 commit comments

Comments
 (0)