Introduce ResultActions.andExpectAll() for soft assertions in MockMvc
Closes gh-26917
This commit is contained in:
@@ -1,117 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.web.servlet;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
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 static final String EOL = "\n";
|
||||
|
||||
private final StubMvcResult stubMvcResult = new StubMvcResult(null, null, null, null, null, null, null);
|
||||
|
||||
|
||||
@Test
|
||||
void softAssertionsWithNoFailures() {
|
||||
ResultMatcher resultMatcher = ResultMatcher.matchAllSoftly(this::doNothing);
|
||||
|
||||
assertThatNoException().isThrownBy(() -> resultMatcher.match(stubMvcResult));
|
||||
}
|
||||
|
||||
@Test
|
||||
void softAssertionsWithOneAssertionError() {
|
||||
String failureMessage = "error";
|
||||
ResultMatcher resultMatcher = ResultMatcher.matchAllSoftly(assertionErrorMatcher(failureMessage));
|
||||
|
||||
assertThatExceptionOfType(AssertionError.class)
|
||||
.isThrownBy(() -> resultMatcher.match(stubMvcResult))
|
||||
.withMessage(failureMessage)
|
||||
.withNoCause()
|
||||
.satisfies(error -> assertThat(error).hasNoSuppressedExceptions());
|
||||
}
|
||||
|
||||
@Test
|
||||
void softAssertionsWithOneRuntimeException() {
|
||||
String failureMessage = "exception";
|
||||
ResultMatcher resultMatcher = ResultMatcher.matchAllSoftly(uncheckedExceptionMatcher(failureMessage));
|
||||
|
||||
assertThatExceptionOfType(RuntimeException.class)
|
||||
.isThrownBy(() -> resultMatcher.match(stubMvcResult))
|
||||
.withMessage(failureMessage)
|
||||
.withNoCause()
|
||||
.satisfies(error -> assertThat(error).hasNoSuppressedExceptions());
|
||||
}
|
||||
|
||||
@Test
|
||||
void softAssertionsWithOneCheckedException() {
|
||||
String failureMessage = "exception";
|
||||
ResultMatcher resultMatcher = ResultMatcher.matchAllSoftly(checkedExceptionMatcher(failureMessage));
|
||||
|
||||
assertThatExceptionOfType(Exception.class)
|
||||
.isThrownBy(() -> resultMatcher.match(stubMvcResult))
|
||||
.withMessage(failureMessage)
|
||||
.withNoCause()
|
||||
.satisfies(exception -> assertThat(exception).hasNoSuppressedExceptions());
|
||||
}
|
||||
|
||||
@Test
|
||||
void softAssertionsWithTwoFailures() {
|
||||
String firstFailure = "firstFailure";
|
||||
String secondFailure = "secondFailure";
|
||||
String thirdFailure = "thirdFailure";
|
||||
ResultMatcher resultMatcher = ResultMatcher.matchAllSoftly(assertionErrorMatcher(firstFailure),
|
||||
checkedExceptionMatcher(secondFailure), uncheckedExceptionMatcher(thirdFailure));
|
||||
|
||||
assertThatExceptionOfType(AssertionError.class)
|
||||
.isThrownBy(() -> resultMatcher.match(stubMvcResult))
|
||||
.withMessage("Multiple Exceptions (3):" + EOL + firstFailure + EOL + secondFailure + EOL + thirdFailure)
|
||||
.satisfies(error -> assertThat(error.getSuppressed()).hasSize(3));
|
||||
}
|
||||
|
||||
private ResultMatcher assertionErrorMatcher(String failureMessage) {
|
||||
return result -> {
|
||||
throw new AssertionError(failureMessage);
|
||||
};
|
||||
}
|
||||
|
||||
private ResultMatcher uncheckedExceptionMatcher(String failureMessage) {
|
||||
return result -> {
|
||||
throw new RuntimeException(failureMessage);
|
||||
};
|
||||
}
|
||||
|
||||
private ResultMatcher checkedExceptionMatcher(String failureMessage) {
|
||||
return result -> {
|
||||
throw new Exception(failureMessage);
|
||||
};
|
||||
}
|
||||
|
||||
void doNothing(MvcResult mvcResult) {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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.
|
||||
@@ -47,11 +47,13 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
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;
|
||||
|
||||
@@ -61,6 +63,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Sam Brannen
|
||||
* @author Sebastien Deleuze
|
||||
* @author Michał Rowicki
|
||||
*/
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@WebAppConfiguration("classpath:META-INF/web-resources")
|
||||
@@ -93,9 +96,38 @@ public class JavaConfigTests {
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user