Polish soft assertions for MockMvc

See gh-26917
This commit is contained in:
Sam Brannen 2021-08-22 18:32:05 +02:00
parent 35bec8102b
commit 5f47d3be22
3 changed files with 55 additions and 41 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,9 +32,11 @@ public interface ResultActions {
/** /**
* Perform an expectation. * Perform an expectation.
* *
* <h4>Example</h4> * <h4>Examples</h4>
*
* <p>You can invoke {@code andExpect()} multiple times.
* <pre class="code"> * <pre class="code">
* static imports: MockMvcRequestBuilders.*, MockMvcResultMatchers.* * // static imports: MockMvcRequestBuilders.*, MockMvcResultMatchers.*
* *
* mockMvc.perform(get("/person/1")) * mockMvc.perform(get("/person/1"))
* .andExpect(status().isOk()) * .andExpect(status().isOk())
@ -42,9 +44,9 @@ public interface ResultActions {
* .andExpect(jsonPath("$.person.name").value("Jason")); * .andExpect(jsonPath("$.person.name").value("Jason"));
* </pre> * </pre>
* *
* <p>Either provide all matchers as a vararg: * <p>You can provide all matchers as a var-arg list with {@code matchAll()}.
* <pre class="code"> * <pre class="code">
* static imports: MockMvcRequestBuilders.*, MockMvcResultMatchers.*, ResultMatcher.matchAll * // static imports: MockMvcRequestBuilders.*, MockMvcResultMatchers.*, ResultMatcher.matchAll
* *
* mockMvc.perform(post("/form")) * mockMvc.perform(post("/form"))
* .andExpect(matchAll( * .andExpect(matchAll(
@ -57,13 +59,14 @@ public interface ResultActions {
* ); * );
* </pre> * </pre>
* *
* <p>Or provide all matchers to be evaluated no matter if one of them fail: * <p>Alternatively, you can provide all matchers to be evaluated using
* <em>soft assertions</em> with {@code matchAllSoftly()}.
* <pre class="code"> * <pre class="code">
* static imports: MockMvcRequestBuilders.*, MockMvcResultMatchers.*, ResultMatcher.matchAllSoftly * // static imports: MockMvcRequestBuilders.*, MockMvcResultMatchers.*, ResultMatcher.matchAllSoftly
* mockMvc.perform(post("/form")) * mockMvc.perform(post("/form"))
* .andExpect(matchAllSoftly( * .andExpect(matchAllSoftly(
* status().isOk(), * status().isOk(),
* redirectedUrl("/person/1") * redirectedUrl("/person/1"))
* ); * );
* </pre> * </pre>
*/ */

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,9 +16,6 @@
package org.springframework.test.web.servlet; package org.springframework.test.web.servlet;
import java.util.ArrayList;
import java.util.List;
/** /**
* A {@code ResultMatcher} matches the result of an executed request against * A {@code ResultMatcher} matches the result of an executed request against
* some expectation. * some expectation.
@ -47,6 +44,7 @@ import java.util.List;
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @author Sam Brannen * @author Sam Brannen
* @author Michał Rowicki
* @since 3.2 * @since 3.2
*/ */
@FunctionalInterface @FunctionalInterface
@ -74,27 +72,29 @@ public interface ResultMatcher {
} }
/** /**
* Static method for matching with an array of result matchers whose assertion failures are caught and stored. * Static method for matching with an array of result matchers whose assertion
* Only when all of them would be called a {@link AssertionError} be thrown containing the error messages of those * failures are caught and stored. Once all matchers have been called, if any
* previously caught assertion failures. * failures occurred, an {@link AssertionError} will be thrown containing the
* error messages of all assertion failures.
* @param matchers the matchers * @param matchers the matchers
* @author Michał Rowicki * @since 5.3.10
* @since 5.2
*/ */
static ResultMatcher matchAllSoftly(ResultMatcher... matchers) { static ResultMatcher matchAllSoftly(ResultMatcher... matchers) {
return result -> { return result -> {
List<String> failedMessages = new ArrayList<>(); String message = "";
for (int i = 0; i < matchers.length; i++) { for (ResultMatcher matcher : matchers) {
ResultMatcher matcher = matchers[i];
try { try {
matcher.match(result); matcher.match(result);
} }
catch (AssertionError assertionException) { catch (Error | Exception ex) {
failedMessages.add("[" + i + "] " + assertionException.getMessage()); if (!message.isEmpty()) {
message += System.lineSeparator();
}
message += ex.getMessage();
} }
} }
if (!failedMessages.isEmpty()) { if (!message.isEmpty()) {
throw new AssertionError(String.join("\n", failedMessages)); throw new AssertionError(message);
} }
}; };
} }

View File

@ -16,51 +16,62 @@
package org.springframework.test.web.servlet; package org.springframework.test.web.servlet;
import org.jetbrains.annotations.NotNull; import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatNoException; import static org.assertj.core.api.Assertions.assertThatNoException;
/**
* Unit tests for {@link ResultMatcher}.
*
* @author Michał Rowicki
* @author Sam Brannen
* @since 5.3.10
*/
class ResultMatcherTests { class ResultMatcherTests {
private final StubMvcResult stubMvcResult = new StubMvcResult(null, null, null, null, null, null, null);
@Test @Test
void whenProvidedMatcherPassesThenSoftAssertionsAlsoPasses() { void softAssertionsWithNoFailures() {
ResultMatcher resultMatcher = ResultMatcher.matchAllSoftly(this::doNothing); ResultMatcher resultMatcher = ResultMatcher.matchAllSoftly(this::doNothing);
StubMvcResult stubMvcResult = new StubMvcResult(null, null, null, null, null, null, null);
assertThatNoException().isThrownBy(() -> resultMatcher.match(stubMvcResult)); assertThatNoException().isThrownBy(() -> resultMatcher.match(stubMvcResult));
} }
@Test @Test
void whenOneOfMatcherFailsThenSoftAssertionFailsWithTheVerySameMessage() { void softAssertionsWithOneFailure() {
String failMessage = "fail message"; String failureMessage = "failure message";
StubMvcResult stubMvcResult = new StubMvcResult(null, null, null, null, null, null, null); ResultMatcher resultMatcher = ResultMatcher.matchAllSoftly(failingMatcher(failureMessage));
ResultMatcher resultMatcher = ResultMatcher.matchAllSoftly(failMatcher(failMessage));
assertThatExceptionOfType(AssertionError.class) assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> resultMatcher.match(stubMvcResult)) .isThrownBy(() -> resultMatcher.match(stubMvcResult))
.withMessage("[0] " + failMessage); .withMessage(failureMessage);
} }
@Test @Test
void whenMultipleMatchersFailsThenSoftAssertionFailsWithOneErrorWithMessageContainingAllErrorMessagesWithTheSameOrder() { void softAssertionsWithTwoFailures() {
String firstFail = "firstFail"; String firstFailure = "firstFailure";
String secondFail = "secondFail"; String secondFailure = "secondFailure";
StubMvcResult stubMvcResult = new StubMvcResult(null, null, null, null, null, null, null); ResultMatcher resultMatcher = ResultMatcher.matchAllSoftly(failingMatcher(firstFailure), exceptionalMatcher(secondFailure));
ResultMatcher resultMatcher = ResultMatcher.matchAllSoftly(failMatcher(firstFail), failMatcher(secondFail));
assertThatExceptionOfType(AssertionError.class) assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> resultMatcher.match(stubMvcResult)) .isThrownBy(() -> resultMatcher.match(stubMvcResult))
.withMessage("[0] " + firstFail + "\n[1] " + secondFail); .withMessage(firstFailure + System.lineSeparator() + secondFailure);
} }
@NotNull private ResultMatcher failingMatcher(String failureMessage) {
private ResultMatcher failMatcher(String failMessage) { return result -> Assertions.fail(failureMessage);
}
private ResultMatcher exceptionalMatcher(String failureMessage) {
return result -> { return result -> {
throw new AssertionError(failMessage); throw new RuntimeException(failureMessage);
}; };
} }
void doNothing(MvcResult mvcResult) {} void doNothing(MvcResult mvcResult) {}
} }