Skip to content

Commit

Permalink
Introduce ResultActions.andExpectAll() for soft assertions in MockMvc
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen authored and lxbzmy committed Mar 26, 2022
1 parent da037b0 commit 4893f65
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.test.web.servlet;

import org.springframework.test.util.ExceptionCollector;

/**
* Allows applying actions, such as expectations, on the result of an executed
* request.
Expand All @@ -25,16 +27,18 @@
* {@link org.springframework.test.web.servlet.result.MockMvcResultHandlers}.
*
* @author Rossen Stoyanchev
* @author Sam Brannen
* @author Michał Rowicki
* @since 3.2
*/
public interface ResultActions {

/**
* Perform an expectation.
*
* <h4>Examples</h4>
*
* <p>You can invoke {@code andExpect()} multiple times.
* <h4>Example</h4>
* <p>You can invoke {@code andExpect()} multiple times as in the following
* example.
* <pre class="code">
* // static imports: MockMvcRequestBuilders.*, MockMvcResultMatchers.*
*
Expand All @@ -44,33 +48,48 @@ public interface ResultActions {
* .andExpect(jsonPath("$.person.name").value("Jason"));
* </pre>
*
* <p>You can provide all matchers as a var-arg list with {@code matchAll()}.
* @see #andExpectAll(ResultMatcher...)
*/
ResultActions andExpect(ResultMatcher matcher) throws Exception;

/**
* Perform multiple expectations, with the guarantee that all expectations
* will be asserted even if one or more expectations fail with an exception.
* <p>If a single {@link Error} or {@link Exception} is thrown, it will
* be rethrown.
* <p>If multiple exceptions are thrown, this method will throw an
* {@link AssertionError} whose error message is a summary of all of the
* exceptions. In addition, each exception will be added as a
* {@linkplain Throwable#addSuppressed(Throwable) suppressed exception} to
* the {@code AssertionError}.
* <p>This feature is similar to the {@code SoftAssertions} support in AssertJ
* and the {@code assertAll()} support in JUnit Jupiter.
*
* <h4>Example</h4>
* <p>Instead of invoking {@code andExpect()} multiple times, you can invoke
* {@code andExpectAll()} as in the following example.
* <pre class="code">
* // static imports: MockMvcRequestBuilders.*, MockMvcResultMatchers.*, ResultMatcher.matchAll
* // static imports: MockMvcRequestBuilders.*, MockMvcResultMatchers.*
*
* mockMvc.perform(post("/form"))
* .andExpect(matchAll(
* mockMvc.perform(get("/person/1"))
* .andExpectAll(
* status().isOk(),
* redirectedUrl("/person/1"),
* model().size(1),
* model().attributeExists("person"),
* flash().attributeCount(1),
* flash().attribute("message", "success!"))
* content().contentType(MediaType.APPLICATION_JSON),
* jsonPath("$.person.name").value("Jason")
* );
* </pre>
*
* <p>Alternatively, you can provide all matchers to be evaluated using
* <em>soft assertions</em> with {@code matchAllSoftly()}.
* <pre class="code">
* // static imports: MockMvcRequestBuilders.*, MockMvcResultMatchers.*, ResultMatcher.matchAllSoftly
* mockMvc.perform(post("/form"))
* .andExpect(matchAllSoftly(
* status().isOk(),
* redirectedUrl("/person/1"))
* );
* </pre>
* @since 5.3.10
* @see #andExpect(ResultMatcher)
*/
ResultActions andExpect(ResultMatcher matcher) throws Exception;
default ResultActions andExpectAll(ResultMatcher... matchers) throws Exception {
ExceptionCollector exceptionCollector = new ExceptionCollector();
for (ResultMatcher matcher : matchers) {
exceptionCollector.execute(() -> this.andExpect(matcher));
}
exceptionCollector.assertEmpty();
return this;
}

/**
* Perform a general action.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

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.
Expand All @@ -40,13 +38,13 @@
* MockMvc mockMvc = webAppContextSetup(wac).build();
*
* mockMvc.perform(get("/form"))
* .andExpect(status().isOk())
* .andExpect(content().mimeType(MediaType.APPLICATION_JSON));
* .andExpectAll(
* status().isOk(),
* content().mimeType(MediaType.APPLICATION_JSON));
* </pre>
*
* @author Rossen Stoyanchev
* @author Sam Brannen
* @author Michał Rowicki
* @since 3.2
*/
@FunctionalInterface
Expand All @@ -64,7 +62,10 @@ public interface ResultMatcher {
* Static method for matching with an array of result matchers.
* @param matchers the matchers
* @since 5.1
* @deprecated as of Spring Framework 5.3.10, in favor of
* {@link ResultActions#andExpectAll(ResultMatcher...)}
*/
@Deprecated
static ResultMatcher matchAll(ResultMatcher... matchers) {
return result -> {
for (ResultMatcher matcher : matchers) {
Expand All @@ -73,22 +74,4 @@ static ResultMatcher matchAll(ResultMatcher... matchers) {
};
}

/**
* Static method for matching with an array of result matchers whose assertion
* failures are caught and stored. Once all matchers have been called, if any
* failures occurred, an {@link AssertionError} will be thrown containing the
* error messages of all assertion failures.
* @param matchers the matchers
* @since 5.3.10
*/
static ResultMatcher matchAllSoftly(ResultMatcher... matchers) {
return result -> {
ExceptionCollector exceptionCollector = new ExceptionCollector();
for (ResultMatcher matcher : matchers) {
exceptionCollector.execute(() -> matcher.match(result));
}
exceptionCollector.assertEmpty();
};
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -47,11 +47,13 @@
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

Expand All @@ -61,6 +63,7 @@
* @author Rossen Stoyanchev
* @author Sam Brannen
* @author Sebastien Deleuze
* @author Michał Rowicki
*/
@ExtendWith(SpringExtension.class)
@WebAppConfiguration("classpath:META-INF/web-resources")
Expand Down Expand Up @@ -93,9 +96,38 @@ public void setup() {
public void person() throws Exception {
this.mockMvc.perform(get("/person/5").accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isOk())
.andExpect(request().asyncNotStarted())
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
.andExpectAll(
status().isOk(),
request().asyncNotStarted(),
content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"),
jsonPath("$.name").value("Joe")
);
}

@Test
public void andExpectAllWithOneFailure() {
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> this.mockMvc.perform(get("/person/5").accept(MediaType.APPLICATION_JSON))
.andExpectAll(
status().isBadGateway(),
request().asyncNotStarted(),
jsonPath("$.name").value("Joe")))
.withMessage("Status expected:<502> but was:<200>")
.satisfies(error -> assertThat(error).hasNoSuppressedExceptions());
}

@Test
public void andExpectAllWithMultipleFailures() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
this.mockMvc.perform(get("/person/5").accept(MediaType.APPLICATION_JSON))
.andExpectAll(
status().isBadGateway(),
request().asyncNotStarted(),
jsonPath("$.name").value("Joe"),
jsonPath("$.name").value("Jane")
))
.withMessage("Multiple Exceptions (2):\nStatus expected:<502> but was:<200>\nJSON path \"$.name\" expected:<Jane> but was:<Joe>")
.satisfies(error -> assertThat(error.getSuppressed()).hasSize(2));
}

@Test
Expand Down
20 changes: 18 additions & 2 deletions src/docs/asciidoc/testing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -7219,8 +7219,9 @@ they must be specified on every request.
[[spring-mvc-test-server-defining-expectations]]
===== Defining Expectations

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

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

You can define multiple expectations by appending `andExpectAll(..)` after performing a
request, as the following example shows. In contrast to `andExpect(..)`,
`andExpectAll(..)` guarantees that all supplied expectations will be asserted and that
all failures will be tracked and reported.

[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
// static import of MockMvcRequestBuilders.* and MockMvcResultMatchers.*
mockMvc.perform(get("/accounts/1")).andExpectAll(
status().isOk(),
content().contentType("application/json;charset=UTF-8"));
----

`MockMvcResultMatchers.*` provides a number of expectations, some of which are further
nested with more detailed expectations.

Expand Down

0 comments on commit 4893f65

Please sign in to comment.