Skip to content

Commit cd078ea

Browse files
committed
Use ExceptionCollector for soft assertions in MockMvc
See gh-26917
1 parent 5f47d3b commit cd078ea

File tree

2 files changed

+56
-24
lines changed

2 files changed

+56
-24
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/ResultMatcher.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.test.web.servlet;
1818

19+
import org.springframework.test.util.ExceptionCollector;
20+
1921
/**
2022
* A {@code ResultMatcher} matches the result of an executed request against
2123
* some expectation.
@@ -81,21 +83,11 @@ static ResultMatcher matchAll(ResultMatcher... matchers) {
8183
*/
8284
static ResultMatcher matchAllSoftly(ResultMatcher... matchers) {
8385
return result -> {
84-
String message = "";
86+
ExceptionCollector exceptionCollector = new ExceptionCollector();
8587
for (ResultMatcher matcher : matchers) {
86-
try {
87-
matcher.match(result);
88-
}
89-
catch (Error | Exception ex) {
90-
if (!message.isEmpty()) {
91-
message += System.lineSeparator();
92-
}
93-
message += ex.getMessage();
94-
}
95-
}
96-
if (!message.isEmpty()) {
97-
throw new AssertionError(message);
88+
exceptionCollector.execute(() -> matcher.match(result));
9889
}
90+
exceptionCollector.assertEmpty();
9991
};
10092
}
10193

spring-test/src/test/java/org/springframework/test/web/servlet/ResultMatcherTests.java

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
package org.springframework.test.web.servlet;
1818

19-
import org.assertj.core.api.Assertions;
2019
import org.junit.jupiter.api.Test;
2120

21+
import static org.assertj.core.api.Assertions.assertThat;
2222
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2323
import static org.assertj.core.api.Assertions.assertThatNoException;
2424

@@ -31,6 +31,8 @@
3131
*/
3232
class ResultMatcherTests {
3333

34+
private static final String EOL = "\n";
35+
3436
private final StubMvcResult stubMvcResult = new StubMvcResult(null, null, null, null, null, null, null);
3537

3638

@@ -42,36 +44,74 @@ void softAssertionsWithNoFailures() {
4244
}
4345

4446
@Test
45-
void softAssertionsWithOneFailure() {
46-
String failureMessage = "failure message";
47-
ResultMatcher resultMatcher = ResultMatcher.matchAllSoftly(failingMatcher(failureMessage));
47+
void softAssertionsWithOneAssertionError() {
48+
String failureMessage = "error";
49+
ResultMatcher resultMatcher = ResultMatcher.matchAllSoftly(assertionErrorMatcher(failureMessage));
4850

4951
assertThatExceptionOfType(AssertionError.class)
5052
.isThrownBy(() -> resultMatcher.match(stubMvcResult))
51-
.withMessage(failureMessage);
53+
.withMessage(failureMessage)
54+
.withNoCause()
55+
.satisfies(error -> assertThat(error).hasNoSuppressedExceptions());
56+
}
57+
58+
@Test
59+
void softAssertionsWithOneRuntimeException() {
60+
String failureMessage = "exception";
61+
ResultMatcher resultMatcher = ResultMatcher.matchAllSoftly(uncheckedExceptionMatcher(failureMessage));
62+
63+
assertThatExceptionOfType(RuntimeException.class)
64+
.isThrownBy(() -> resultMatcher.match(stubMvcResult))
65+
.withMessage(failureMessage)
66+
.withNoCause()
67+
.satisfies(error -> assertThat(error).hasNoSuppressedExceptions());
68+
}
69+
70+
@Test
71+
void softAssertionsWithOneCheckedException() {
72+
String failureMessage = "exception";
73+
ResultMatcher resultMatcher = ResultMatcher.matchAllSoftly(checkedExceptionMatcher(failureMessage));
74+
75+
assertThatExceptionOfType(Exception.class)
76+
.isThrownBy(() -> resultMatcher.match(stubMvcResult))
77+
.withMessage(failureMessage)
78+
.withNoCause()
79+
.satisfies(exception -> assertThat(exception).hasNoSuppressedExceptions());
5280
}
5381

5482
@Test
5583
void softAssertionsWithTwoFailures() {
5684
String firstFailure = "firstFailure";
5785
String secondFailure = "secondFailure";
58-
ResultMatcher resultMatcher = ResultMatcher.matchAllSoftly(failingMatcher(firstFailure), exceptionalMatcher(secondFailure));
86+
String thirdFailure = "thirdFailure";
87+
ResultMatcher resultMatcher = ResultMatcher.matchAllSoftly(assertionErrorMatcher(firstFailure),
88+
checkedExceptionMatcher(secondFailure), uncheckedExceptionMatcher(thirdFailure));
5989

6090
assertThatExceptionOfType(AssertionError.class)
6191
.isThrownBy(() -> resultMatcher.match(stubMvcResult))
62-
.withMessage(firstFailure + System.lineSeparator() + secondFailure);
92+
.withMessage("Multiple Exceptions (3):" + EOL + firstFailure + EOL + secondFailure + EOL + thirdFailure)
93+
.satisfies(error -> assertThat(error.getSuppressed()).hasSize(3));
6394
}
6495

65-
private ResultMatcher failingMatcher(String failureMessage) {
66-
return result -> Assertions.fail(failureMessage);
96+
private ResultMatcher assertionErrorMatcher(String failureMessage) {
97+
return result -> {
98+
throw new AssertionError(failureMessage);
99+
};
67100
}
68101

69-
private ResultMatcher exceptionalMatcher(String failureMessage) {
102+
private ResultMatcher uncheckedExceptionMatcher(String failureMessage) {
70103
return result -> {
71104
throw new RuntimeException(failureMessage);
72105
};
73106
}
74107

75-
void doNothing(MvcResult mvcResult) {}
108+
private ResultMatcher checkedExceptionMatcher(String failureMessage) {
109+
return result -> {
110+
throw new Exception(failureMessage);
111+
};
112+
}
113+
114+
void doNothing(MvcResult mvcResult) {
115+
}
76116

77117
}

0 commit comments

Comments
 (0)