Use ExceptionCollector for soft assertions in MockMvc

See gh-26917
This commit is contained in:
Sam Brannen
2021-08-22 19:03:32 +02:00
parent 5f47d3be22
commit cd078eaad8
2 changed files with 56 additions and 24 deletions

View File

@@ -16,6 +16,8 @@
package org.springframework.test.web.servlet;
import org.springframework.test.util.ExceptionCollector;
/**
* A {@code ResultMatcher} matches the result of an executed request against
* some expectation.
@@ -81,21 +83,11 @@ public interface ResultMatcher {
*/
static ResultMatcher matchAllSoftly(ResultMatcher... matchers) {
return result -> {
String message = "";
ExceptionCollector exceptionCollector = new ExceptionCollector();
for (ResultMatcher matcher : matchers) {
try {
matcher.match(result);
}
catch (Error | Exception ex) {
if (!message.isEmpty()) {
message += System.lineSeparator();
}
message += ex.getMessage();
}
}
if (!message.isEmpty()) {
throw new AssertionError(message);
exceptionCollector.execute(() -> matcher.match(result));
}
exceptionCollector.assertEmpty();
};
}