45
45
*
46
46
* @author Phillip Webb
47
47
* @author Andy Wilkinson
48
+ * @author Camille Vienot
48
49
* @since 1.4.0
49
50
*/
50
51
public class JsonContentAssert extends AbstractAssert <JsonContentAssert , CharSequence > {
@@ -96,7 +97,8 @@ public JsonContentAssert isEqualTo(Object expected) {
96
97
if (expected instanceof Resource ) {
97
98
return isEqualToJson ((Resource ) expected );
98
99
}
99
- throw new AssertionError ("Unsupport type for JSON assert " + expected .getClass ());
100
+ failWithMessage ("Unsupport type for JSON assert {}" , expected .getClass ());
101
+ return null ;
100
102
}
101
103
102
104
/**
@@ -432,7 +434,8 @@ public JsonContentAssert isNotEqualTo(Object expected) {
432
434
if (expected instanceof Resource ) {
433
435
return isNotEqualToJson ((Resource ) expected );
434
436
}
435
- throw new AssertionError ("Unsupport type for JSON assert " + expected .getClass ());
437
+ failWithMessage ("Unsupport type for JSON assert {]" , expected .getClass ());
438
+ return null ;
436
439
}
437
440
438
441
/**
@@ -1031,14 +1034,14 @@ private JSONCompareResult compareForNull(CharSequence expectedJson) {
1031
1034
1032
1035
private JsonContentAssert assertNotFailed (JSONCompareResult result ) {
1033
1036
if (result .failed ()) {
1034
- throw new AssertionError ("JSON Comparison failure: " + result .getMessage ());
1037
+ failWithMessage ("JSON Comparison failure: {}" , result .getMessage ());
1035
1038
}
1036
1039
return this ;
1037
1040
}
1038
1041
1039
1042
private JsonContentAssert assertNotPassed (JSONCompareResult result ) {
1040
1043
if (result .passed ()) {
1041
- throw new AssertionError ("JSON Comparison failure: " + result .getMessage ());
1044
+ failWithMessage ("JSON Comparison failure: {}" , result .getMessage ());
1042
1045
}
1043
1046
return this ;
1044
1047
}
@@ -1064,32 +1067,32 @@ public void assertHasEmptyValue() {
1064
1067
if (ObjectUtils .isEmpty (getValue (false )) || isIndefiniteAndEmpty ()) {
1065
1068
return ;
1066
1069
}
1067
- throw new AssertionError (getExpectedValueMessage ("an empty value" ));
1070
+ failWithMessage (getExpectedValueMessage ("an empty value" ));
1068
1071
}
1069
1072
1070
1073
public void assertDoesNotHaveEmptyValue () {
1071
1074
if (!ObjectUtils .isEmpty (getValue (false ))) {
1072
1075
return ;
1073
1076
}
1074
- throw new AssertionError (getExpectedValueMessage ("a non-empty value" ));
1077
+ failWithMessage (getExpectedValueMessage ("a non-empty value" ));
1075
1078
1076
1079
}
1077
1080
1078
1081
public void assertHasValue (Class <?> type , String expectedDescription ) {
1079
1082
Object value = getValue (true );
1080
1083
if (value == null || isIndefiniteAndEmpty ()) {
1081
- throw new AssertionError (getNoValueMessage ());
1084
+ failWithMessage (getNoValueMessage ());
1082
1085
}
1083
1086
if (type != null && !type .isInstance (value )) {
1084
- throw new AssertionError (getExpectedValueMessage (expectedDescription ));
1087
+ failWithMessage (getExpectedValueMessage (expectedDescription ));
1085
1088
}
1086
1089
}
1087
1090
1088
1091
public void assertDoesNotHaveValue () {
1089
1092
if (getValue (false ) == null || isIndefiniteAndEmpty ()) {
1090
1093
return ;
1091
1094
}
1092
- throw new AssertionError (getExpectedValueMessage ("no value" ));
1095
+ failWithMessage (getExpectedValueMessage ("no value" ));
1093
1096
}
1094
1097
1095
1098
private boolean isIndefiniteAndEmpty () {
@@ -1110,10 +1113,10 @@ public Object getValue(boolean required) {
1110
1113
return this .jsonPath .read ((json != null ) ? json .toString () : null );
1111
1114
}
1112
1115
catch (Exception ex ) {
1113
- if (! required ) {
1114
- return null ;
1116
+ if (required ) {
1117
+ failWithMessage ( "{}. {}" , getNoValueMessage (), ex . getMessage ()) ;
1115
1118
}
1116
- throw new AssertionError ( getNoValueMessage () + ". " + ex . getMessage ()) ;
1119
+ return null ;
1117
1120
}
1118
1121
}
1119
1122
0 commit comments