Use assertJ to generate AssertionError and filter its stacktrace
Closes gh-15569
This commit is contained in:
parent
51936e1a5c
commit
ff8edf8d98
|
@ -45,6 +45,7 @@ import org.springframework.util.StringUtils;
|
||||||
*
|
*
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
|
* @author Camille Vienot
|
||||||
* @since 1.4.0
|
* @since 1.4.0
|
||||||
*/
|
*/
|
||||||
public class JsonContentAssert extends AbstractAssert<JsonContentAssert, CharSequence> {
|
public class JsonContentAssert extends AbstractAssert<JsonContentAssert, CharSequence> {
|
||||||
|
@ -96,7 +97,8 @@ public class JsonContentAssert extends AbstractAssert<JsonContentAssert, CharSeq
|
||||||
if (expected instanceof Resource) {
|
if (expected instanceof Resource) {
|
||||||
return isEqualToJson((Resource) expected);
|
return isEqualToJson((Resource) expected);
|
||||||
}
|
}
|
||||||
throw new AssertionError("Unsupport type for JSON assert " + expected.getClass());
|
failWithMessage("Unsupport type for JSON assert {}", expected.getClass());
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -432,7 +434,8 @@ public class JsonContentAssert extends AbstractAssert<JsonContentAssert, CharSeq
|
||||||
if (expected instanceof Resource) {
|
if (expected instanceof Resource) {
|
||||||
return isNotEqualToJson((Resource) expected);
|
return isNotEqualToJson((Resource) expected);
|
||||||
}
|
}
|
||||||
throw new AssertionError("Unsupport type for JSON assert " + expected.getClass());
|
failWithMessage("Unsupport type for JSON assert {]", expected.getClass());
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1031,14 +1034,14 @@ public class JsonContentAssert extends AbstractAssert<JsonContentAssert, CharSeq
|
||||||
|
|
||||||
private JsonContentAssert assertNotFailed(JSONCompareResult result) {
|
private JsonContentAssert assertNotFailed(JSONCompareResult result) {
|
||||||
if (result.failed()) {
|
if (result.failed()) {
|
||||||
throw new AssertionError("JSON Comparison failure: " + result.getMessage());
|
failWithMessage("JSON Comparison failure: {}", result.getMessage());
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private JsonContentAssert assertNotPassed(JSONCompareResult result) {
|
private JsonContentAssert assertNotPassed(JSONCompareResult result) {
|
||||||
if (result.passed()) {
|
if (result.passed()) {
|
||||||
throw new AssertionError("JSON Comparison failure: " + result.getMessage());
|
failWithMessage("JSON Comparison failure: {}", result.getMessage());
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -1064,24 +1067,24 @@ public class JsonContentAssert extends AbstractAssert<JsonContentAssert, CharSeq
|
||||||
if (ObjectUtils.isEmpty(getValue(false)) || isIndefiniteAndEmpty()) {
|
if (ObjectUtils.isEmpty(getValue(false)) || isIndefiniteAndEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
throw new AssertionError(getExpectedValueMessage("an empty value"));
|
failWithMessage(getExpectedValueMessage("an empty value"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void assertDoesNotHaveEmptyValue() {
|
public void assertDoesNotHaveEmptyValue() {
|
||||||
if (!ObjectUtils.isEmpty(getValue(false))) {
|
if (!ObjectUtils.isEmpty(getValue(false))) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
throw new AssertionError(getExpectedValueMessage("a non-empty value"));
|
failWithMessage(getExpectedValueMessage("a non-empty value"));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void assertHasValue(Class<?> type, String expectedDescription) {
|
public void assertHasValue(Class<?> type, String expectedDescription) {
|
||||||
Object value = getValue(true);
|
Object value = getValue(true);
|
||||||
if (value == null || isIndefiniteAndEmpty()) {
|
if (value == null || isIndefiniteAndEmpty()) {
|
||||||
throw new AssertionError(getNoValueMessage());
|
failWithMessage(getNoValueMessage());
|
||||||
}
|
}
|
||||||
if (type != null && !type.isInstance(value)) {
|
if (type != null && !type.isInstance(value)) {
|
||||||
throw new AssertionError(getExpectedValueMessage(expectedDescription));
|
failWithMessage(getExpectedValueMessage(expectedDescription));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1089,7 +1092,7 @@ public class JsonContentAssert extends AbstractAssert<JsonContentAssert, CharSeq
|
||||||
if (getValue(false) == null || isIndefiniteAndEmpty()) {
|
if (getValue(false) == null || isIndefiniteAndEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
throw new AssertionError(getExpectedValueMessage("no value"));
|
failWithMessage(getExpectedValueMessage("no value"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isIndefiniteAndEmpty() {
|
private boolean isIndefiniteAndEmpty() {
|
||||||
|
@ -1110,10 +1113,10 @@ public class JsonContentAssert extends AbstractAssert<JsonContentAssert, CharSeq
|
||||||
return this.jsonPath.read((json != null) ? json.toString() : null);
|
return this.jsonPath.read((json != null) ? json.toString() : null);
|
||||||
}
|
}
|
||||||
catch (Exception ex) {
|
catch (Exception ex) {
|
||||||
if (!required) {
|
if (required) {
|
||||||
return null;
|
failWithMessage("{}. {}", getNoValueMessage(), ex.getMessage());
|
||||||
}
|
}
|
||||||
throw new AssertionError(getNoValueMessage() + ". " + ex.getMessage());
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue