Skip to content

Commit 35bec81

Browse files
wyhasanynguyensach
authored andcommitted
Introduce soft assertions for MockMvc
It happens very often that MockMvc is used in heavyweight integration tests. It's no use to waste time to check if another condition has been fixed or not. Soft assertions help a lot by checking all conditions at once even if one of them fails. See gh-26917 Co-authored-by: Sach Nguyen <[email protected]>
1 parent 4c153b8 commit 35bec81

File tree

3 files changed

+106
-1
lines changed

3 files changed

+106
-1
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public interface ResultActions {
4242
* .andExpect(jsonPath("$.person.name").value("Jason"));
4343
* </pre>
4444
*
45-
* <p>Or alternatively provide all matchers as a vararg:
45+
* <p>Either provide all matchers as a vararg:
4646
* <pre class="code">
4747
* static imports: MockMvcRequestBuilders.*, MockMvcResultMatchers.*, ResultMatcher.matchAll
4848
*
@@ -56,6 +56,16 @@ public interface ResultActions {
5656
* flash().attribute("message", "success!"))
5757
* );
5858
* </pre>
59+
*
60+
* <p>Or provide all matchers to be evaluated no matter if one of them fail:
61+
* <pre class="code">
62+
* static imports: MockMvcRequestBuilders.*, MockMvcResultMatchers.*, ResultMatcher.matchAllSoftly
63+
* mockMvc.perform(post("/form"))
64+
* .andExpect(matchAllSoftly(
65+
* status().isOk(),
66+
* redirectedUrl("/person/1")
67+
* );
68+
* </pre>
5969
*/
6070
ResultActions andExpect(ResultMatcher matcher) throws Exception;
6171

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

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

19+
import java.util.ArrayList;
20+
import java.util.List;
21+
1922
/**
2023
* A {@code ResultMatcher} matches the result of an executed request against
2124
* some expectation.
@@ -70,4 +73,30 @@ static ResultMatcher matchAll(ResultMatcher... matchers) {
7073
};
7174
}
7275

76+
/**
77+
* Static method for matching with an array of result matchers whose assertion failures are caught and stored.
78+
* Only when all of them would be called a {@link AssertionError} be thrown containing the error messages of those
79+
* previously caught assertion failures.
80+
* @param matchers the matchers
81+
* @author Michał Rowicki
82+
* @since 5.2
83+
*/
84+
static ResultMatcher matchAllSoftly(ResultMatcher... matchers) {
85+
return result -> {
86+
List<String> failedMessages = new ArrayList<>();
87+
for (int i = 0; i < matchers.length; i++) {
88+
ResultMatcher matcher = matchers[i];
89+
try {
90+
matcher.match(result);
91+
}
92+
catch (AssertionError assertionException) {
93+
failedMessages.add("[" + i + "] " + assertionException.getMessage());
94+
}
95+
}
96+
if (!failedMessages.isEmpty()) {
97+
throw new AssertionError(String.join("\n", failedMessages));
98+
}
99+
};
100+
}
101+
73102
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2002-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.web.servlet;
18+
19+
import org.jetbrains.annotations.NotNull;
20+
import org.junit.jupiter.api.Test;
21+
22+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
23+
import static org.assertj.core.api.Assertions.assertThatNoException;
24+
25+
class ResultMatcherTests {
26+
27+
@Test
28+
void whenProvidedMatcherPassesThenSoftAssertionsAlsoPasses() {
29+
ResultMatcher resultMatcher = ResultMatcher.matchAllSoftly(this::doNothing);
30+
StubMvcResult stubMvcResult = new StubMvcResult(null, null, null, null, null, null, null);
31+
32+
assertThatNoException().isThrownBy(() -> resultMatcher.match(stubMvcResult));
33+
}
34+
35+
@Test
36+
void whenOneOfMatcherFailsThenSoftAssertionFailsWithTheVerySameMessage() {
37+
String failMessage = "fail message";
38+
StubMvcResult stubMvcResult = new StubMvcResult(null, null, null, null, null, null, null);
39+
ResultMatcher resultMatcher = ResultMatcher.matchAllSoftly(failMatcher(failMessage));
40+
41+
assertThatExceptionOfType(AssertionError.class)
42+
.isThrownBy(() -> resultMatcher.match(stubMvcResult))
43+
.withMessage("[0] " + failMessage);
44+
}
45+
46+
@Test
47+
void whenMultipleMatchersFailsThenSoftAssertionFailsWithOneErrorWithMessageContainingAllErrorMessagesWithTheSameOrder() {
48+
String firstFail = "firstFail";
49+
String secondFail = "secondFail";
50+
StubMvcResult stubMvcResult = new StubMvcResult(null, null, null, null, null, null, null);
51+
ResultMatcher resultMatcher = ResultMatcher.matchAllSoftly(failMatcher(firstFail), failMatcher(secondFail));
52+
53+
assertThatExceptionOfType(AssertionError.class)
54+
.isThrownBy(() -> resultMatcher.match(stubMvcResult))
55+
.withMessage("[0] " + firstFail + "\n[1] " + secondFail);
56+
}
57+
58+
@NotNull
59+
private ResultMatcher failMatcher(String failMessage) {
60+
return result -> {
61+
throw new AssertionError(failMessage);
62+
};
63+
}
64+
65+
void doNothing(MvcResult mvcResult) {}
66+
}

0 commit comments

Comments
 (0)