Skip to content

Commit dd9b99e

Browse files
committed
Introduce ResultActions.andExpectAll() for soft assertions in MockMvc
Closes gh-26917
1 parent cd078ea commit dd9b99e

File tree

5 files changed

+102
-169
lines changed

5 files changed

+102
-169
lines changed

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

Lines changed: 42 additions & 23 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
* Allows applying actions, such as expectations, on the result of an executed
2123
* request.
@@ -25,16 +27,18 @@
2527
* {@link org.springframework.test.web.servlet.result.MockMvcResultHandlers}.
2628
*
2729
* @author Rossen Stoyanchev
30+
* @author Sam Brannen
31+
* @author Michał Rowicki
2832
* @since 3.2
2933
*/
3034
public interface ResultActions {
3135

3236
/**
3337
* Perform an expectation.
3438
*
35-
* <h4>Examples</h4>
36-
*
37-
* <p>You can invoke {@code andExpect()} multiple times.
39+
* <h4>Example</h4>
40+
* <p>You can invoke {@code andExpect()} multiple times as in the following
41+
* example.
3842
* <pre class="code">
3943
* // static imports: MockMvcRequestBuilders.*, MockMvcResultMatchers.*
4044
*
@@ -44,33 +48,48 @@ public interface ResultActions {
4448
* .andExpect(jsonPath("$.person.name").value("Jason"));
4549
* </pre>
4650
*
47-
* <p>You can provide all matchers as a var-arg list with {@code matchAll()}.
51+
* @see #andExpectAll(ResultMatcher...)
52+
*/
53+
ResultActions andExpect(ResultMatcher matcher) throws Exception;
54+
55+
/**
56+
* Perform multiple expectations, with the guarantee that all expectations
57+
* will be asserted even if one or more expectations fail with an exception.
58+
* <p>If a single {@link Error} or {@link Exception} is thrown, it will
59+
* be rethrown.
60+
* <p>If multiple exceptions are thrown, this method will throw an
61+
* {@link AssertionError} whose error message is a summary of all of the
62+
* exceptions. In addition, each exception will be added as a
63+
* {@linkplain Throwable#addSuppressed(Throwable) suppressed exception} to
64+
* the {@code AssertionError}.
65+
* <p>This feature is similar to the {@code SoftAssertions} support in AssertJ
66+
* and the {@code assertAll()} support in JUnit Jupiter.
67+
*
68+
* <h4>Example</h4>
69+
* <p>Instead of invoking {@code andExpect()} multiple times, you can invoke
70+
* {@code andExpectAll()} as in the following example.
4871
* <pre class="code">
49-
* // static imports: MockMvcRequestBuilders.*, MockMvcResultMatchers.*, ResultMatcher.matchAll
72+
* // static imports: MockMvcRequestBuilders.*, MockMvcResultMatchers.*
5073
*
51-
* mockMvc.perform(post("/form"))
52-
* .andExpect(matchAll(
74+
* mockMvc.perform(get("/person/1"))
75+
* .andExpectAll(
5376
* status().isOk(),
54-
* redirectedUrl("/person/1"),
55-
* model().size(1),
56-
* model().attributeExists("person"),
57-
* flash().attributeCount(1),
58-
* flash().attribute("message", "success!"))
77+
* content().contentType(MediaType.APPLICATION_JSON),
78+
* jsonPath("$.person.name").value("Jason")
5979
* );
6080
* </pre>
6181
*
62-
* <p>Alternatively, you can provide all matchers to be evaluated using
63-
* <em>soft assertions</em> with {@code matchAllSoftly()}.
64-
* <pre class="code">
65-
* // static imports: MockMvcRequestBuilders.*, MockMvcResultMatchers.*, ResultMatcher.matchAllSoftly
66-
* mockMvc.perform(post("/form"))
67-
* .andExpect(matchAllSoftly(
68-
* status().isOk(),
69-
* redirectedUrl("/person/1"))
70-
* );
71-
* </pre>
82+
* @since 5.3.10
83+
* @see #andExpect(ResultMatcher)
7284
*/
73-
ResultActions andExpect(ResultMatcher matcher) throws Exception;
85+
default ResultActions andExpectAll(ResultMatcher... matchers) throws Exception {
86+
ExceptionCollector exceptionCollector = new ExceptionCollector();
87+
for (ResultMatcher matcher : matchers) {
88+
exceptionCollector.execute(() -> this.andExpect(matcher));
89+
}
90+
exceptionCollector.assertEmpty();
91+
return this;
92+
}
7493

7594
/**
7695
* Perform a general action.

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

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

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

19-
import org.springframework.test.util.ExceptionCollector;
20-
2119
/**
2220
* A {@code ResultMatcher} matches the result of an executed request against
2321
* some expectation.
@@ -40,13 +38,13 @@
4038
* MockMvc mockMvc = webAppContextSetup(wac).build();
4139
*
4240
* mockMvc.perform(get("/form"))
43-
* .andExpect(status().isOk())
44-
* .andExpect(content().mimeType(MediaType.APPLICATION_JSON));
41+
* .andExpectAll(
42+
* status().isOk(),
43+
* content().mimeType(MediaType.APPLICATION_JSON));
4544
* </pre>
4645
*
4746
* @author Rossen Stoyanchev
4847
* @author Sam Brannen
49-
* @author Michał Rowicki
5048
* @since 3.2
5149
*/
5250
@FunctionalInterface
@@ -64,7 +62,10 @@ public interface ResultMatcher {
6462
* Static method for matching with an array of result matchers.
6563
* @param matchers the matchers
6664
* @since 5.1
65+
* @deprecated as of Spring Framework 5.3.10, in favor of
66+
* {@link ResultActions#andExpectAll(ResultMatcher...)}
6767
*/
68+
@Deprecated
6869
static ResultMatcher matchAll(ResultMatcher... matchers) {
6970
return result -> {
7071
for (ResultMatcher matcher : matchers) {
@@ -73,22 +74,4 @@ static ResultMatcher matchAll(ResultMatcher... matchers) {
7374
};
7475
}
7576

76-
/**
77-
* Static method for matching with an array of result matchers whose assertion
78-
* failures are caught and stored. Once all matchers have been called, if any
79-
* failures occurred, an {@link AssertionError} will be thrown containing the
80-
* error messages of all assertion failures.
81-
* @param matchers the matchers
82-
* @since 5.3.10
83-
*/
84-
static ResultMatcher matchAllSoftly(ResultMatcher... matchers) {
85-
return result -> {
86-
ExceptionCollector exceptionCollector = new ExceptionCollector();
87-
for (ResultMatcher matcher : matchers) {
88-
exceptionCollector.execute(() -> matcher.match(result));
89-
}
90-
exceptionCollector.assertEmpty();
91-
};
92-
}
93-
9477
}

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

Lines changed: 0 additions & 117 deletions
This file was deleted.

spring-test/src/test/java/org/springframework/test/web/servlet/samples/context/JavaConfigTests.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -47,11 +47,13 @@
4747
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
4848

4949
import static org.assertj.core.api.Assertions.assertThat;
50+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
5051
import static org.mockito.BDDMockito.given;
5152
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
5253
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
5354
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
5455
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
56+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
5557
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
5658
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
5759

@@ -61,6 +63,7 @@
6163
* @author Rossen Stoyanchev
6264
* @author Sam Brannen
6365
* @author Sebastien Deleuze
66+
* @author Michał Rowicki
6467
*/
6568
@ExtendWith(SpringExtension.class)
6669
@WebAppConfiguration("classpath:META-INF/web-resources")
@@ -93,9 +96,38 @@ public void setup() {
9396
public void person() throws Exception {
9497
this.mockMvc.perform(get("/person/5").accept(MediaType.APPLICATION_JSON))
9598
.andDo(print())
96-
.andExpect(status().isOk())
97-
.andExpect(request().asyncNotStarted())
98-
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
99+
.andExpectAll(
100+
status().isOk(),
101+
request().asyncNotStarted(),
102+
content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"),
103+
jsonPath("$.name").value("Joe")
104+
);
105+
}
106+
107+
@Test
108+
public void andExpectAllWithOneFailure() {
109+
assertThatExceptionOfType(AssertionError.class)
110+
.isThrownBy(() -> this.mockMvc.perform(get("/person/5").accept(MediaType.APPLICATION_JSON))
111+
.andExpectAll(
112+
status().isBadGateway(),
113+
request().asyncNotStarted(),
114+
jsonPath("$.name").value("Joe")))
115+
.withMessage("Status expected:<502> but was:<200>")
116+
.satisfies(error -> assertThat(error).hasNoSuppressedExceptions());
117+
}
118+
119+
@Test
120+
public void andExpectAllWithMultipleFailures() {
121+
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
122+
this.mockMvc.perform(get("/person/5").accept(MediaType.APPLICATION_JSON))
123+
.andExpectAll(
124+
status().isBadGateway(),
125+
request().asyncNotStarted(),
126+
jsonPath("$.name").value("Joe"),
127+
jsonPath("$.name").value("Jane")
128+
))
129+
.withMessage("Multiple Exceptions (2):\nStatus expected:<502> but was:<200>\nJSON path \"$.name\" expected:<Jane> but was:<Joe>")
130+
.satisfies(error -> assertThat(error.getSuppressed()).hasSize(2));
99131
}
100132

101133
@Test

src/docs/asciidoc/testing.adoc

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7219,8 +7219,9 @@ they must be specified on every request.
72197219
[[spring-mvc-test-server-defining-expectations]]
72207220
===== Defining Expectations
72217221

7222-
You can define expectations by appending one or more `.andExpect(..)` calls after
7223-
performing a request, as the following example shows:
7222+
You can define expectations by appending one or more `andExpect(..)` calls after
7223+
performing a request, as the following example shows. As soon as one expectation fails,
7224+
no other expectations will be asserted.
72247225

72257226
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
72267227
.Java
@@ -7240,6 +7241,21 @@ performing a request, as the following example shows:
72407241
}
72417242
----
72427243

7244+
You can define multiple expectations by appending `andExpectAll(..)` after performing a
7245+
request, as the following example shows. In contrast to `andExpect(..)`,
7246+
`andExpectAll(..)` guarantees that all supplied expectations will be asserted and that
7247+
all failures will be tracked and reported.
7248+
7249+
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
7250+
.Java
7251+
----
7252+
// static import of MockMvcRequestBuilders.* and MockMvcResultMatchers.*
7253+
7254+
mockMvc.perform(get("/accounts/1")).andExpectAll(
7255+
status().isOk(),
7256+
content().contentType("application/json;charset=UTF-8"));
7257+
----
7258+
72437259
`MockMvcResultMatchers.*` provides a number of expectations, some of which are further
72447260
nested with more detailed expectations.
72457261

0 commit comments

Comments
 (0)