Skip to content

Commit

Permalink
Polish soft assertions for MockMvc
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Aug 23, 2021
1 parent 781416e commit ddccf24
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,19 +32,21 @@ public interface ResultActions {
/**
* Perform an expectation.
*
* <h4>Example</h4>
* <h4>Examples</h4>
*
* <p>You can invoke {@code andExpect()} multiple times.
* <pre class="code">
* static imports: MockMvcRequestBuilders.*, MockMvcResultMatchers.*
* // static imports: MockMvcRequestBuilders.*, MockMvcResultMatchers.*
*
* mockMvc.perform(get("/person/1"))
* .andExpect(status().isOk())
* .andExpect(content().contentType(MediaType.APPLICATION_JSON))
* .andExpect(jsonPath("$.person.name").value("Jason"));
* </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">
* static imports: MockMvcRequestBuilders.*, MockMvcResultMatchers.*, ResultMatcher.matchAll
* // static imports: MockMvcRequestBuilders.*, MockMvcResultMatchers.*, ResultMatcher.matchAll
*
* mockMvc.perform(post("/form"))
* .andExpect(matchAll(
Expand All @@ -57,13 +59,14 @@ public interface ResultActions {
* );
* </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">
* static imports: MockMvcRequestBuilders.*, MockMvcResultMatchers.*, ResultMatcher.matchAllSoftly
* // static imports: MockMvcRequestBuilders.*, MockMvcResultMatchers.*, ResultMatcher.matchAllSoftly
* mockMvc.perform(post("/form"))
* .andExpect(matchAllSoftly(
* status().isOk(),
* redirectedUrl("/person/1")
* redirectedUrl("/person/1"))
* );
* </pre>
*/
Expand Down
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 All @@ -16,9 +16,6 @@

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
* some expectation.
Expand Down Expand Up @@ -47,6 +44,7 @@
*
* @author Rossen Stoyanchev
* @author Sam Brannen
* @author Michał Rowicki
* @since 3.2
*/
@FunctionalInterface
Expand Down Expand Up @@ -74,27 +72,29 @@ static ResultMatcher matchAll(ResultMatcher... matchers) {
}

/**
* Static method for matching with an array of result matchers whose assertion failures are caught and stored.
* Only when all of them would be called a {@link AssertionError} be thrown containing the error messages of those
* previously caught assertion failures.
* 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
* @author Michał Rowicki
* @since 5.2
* @since 5.3.10
*/
static ResultMatcher matchAllSoftly(ResultMatcher... matchers) {
return result -> {
List<String> failedMessages = new ArrayList<>();
for (int i = 0; i < matchers.length; i++) {
ResultMatcher matcher = matchers[i];
String message = "";
for (ResultMatcher matcher : matchers) {
try {
matcher.match(result);
}
catch (AssertionError assertionException) {
failedMessages.add("[" + i + "] " + assertionException.getMessage());
catch (Error | Exception ex) {
if (!message.isEmpty()) {
message += System.lineSeparator();
}
message += ex.getMessage();
}
}
if (!failedMessages.isEmpty()) {
throw new AssertionError(String.join("\n", failedMessages));
if (!message.isEmpty()) {
throw new AssertionError(message);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,62 @@

package org.springframework.test.web.servlet;

import org.jetbrains.annotations.NotNull;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
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 {

private final StubMvcResult stubMvcResult = new StubMvcResult(null, null, null, null, null, null, null);


@Test
void whenProvidedMatcherPassesThenSoftAssertionsAlsoPasses() {
void softAssertionsWithNoFailures() {
ResultMatcher resultMatcher = ResultMatcher.matchAllSoftly(this::doNothing);
StubMvcResult stubMvcResult = new StubMvcResult(null, null, null, null, null, null, null);

assertThatNoException().isThrownBy(() -> resultMatcher.match(stubMvcResult));
}

@Test
void whenOneOfMatcherFailsThenSoftAssertionFailsWithTheVerySameMessage() {
String failMessage = "fail message";
StubMvcResult stubMvcResult = new StubMvcResult(null, null, null, null, null, null, null);
ResultMatcher resultMatcher = ResultMatcher.matchAllSoftly(failMatcher(failMessage));
void softAssertionsWithOneFailure() {
String failureMessage = "failure message";
ResultMatcher resultMatcher = ResultMatcher.matchAllSoftly(failingMatcher(failureMessage));

assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> resultMatcher.match(stubMvcResult))
.withMessage("[0] " + failMessage);
.withMessage(failureMessage);
}

@Test
void whenMultipleMatchersFailsThenSoftAssertionFailsWithOneErrorWithMessageContainingAllErrorMessagesWithTheSameOrder() {
String firstFail = "firstFail";
String secondFail = "secondFail";
StubMvcResult stubMvcResult = new StubMvcResult(null, null, null, null, null, null, null);
ResultMatcher resultMatcher = ResultMatcher.matchAllSoftly(failMatcher(firstFail), failMatcher(secondFail));
void softAssertionsWithTwoFailures() {
String firstFailure = "firstFailure";
String secondFailure = "secondFailure";
ResultMatcher resultMatcher = ResultMatcher.matchAllSoftly(failingMatcher(firstFailure), exceptionalMatcher(secondFailure));

assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> resultMatcher.match(stubMvcResult))
.withMessage("[0] " + firstFail + "\n[1] " + secondFail);
.withMessage(firstFailure + System.lineSeparator() + secondFailure);
}

@NotNull
private ResultMatcher failMatcher(String failMessage) {
private ResultMatcher failingMatcher(String failureMessage) {
return result -> Assertions.fail(failureMessage);
}

private ResultMatcher exceptionalMatcher(String failureMessage) {
return result -> {
throw new AssertionError(failMessage);
throw new RuntimeException(failureMessage);
};
}

void doNothing(MvcResult mvcResult) {}

}

0 comments on commit ddccf24

Please sign in to comment.