Skip to content

Commit 88a5b4e

Browse files
committed
Merge branch '6.2.x'
2 parents 1ef7e70 + 52006b7 commit 88a5b4e

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

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

+13
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.assertj.core.api.AbstractStringAssert;
2323
import org.assertj.core.api.Assertions;
2424
import org.assertj.core.api.ByteArrayAssert;
25+
import org.assertj.core.api.StringAssert;
2526

2627
import org.springframework.lang.Nullable;
2728
import org.springframework.mock.web.MockHttpServletResponse;
@@ -163,4 +164,16 @@ public SELF hasRedirectedUrl(@Nullable String redirectedUrl) {
163164
return this.myself;
164165
}
165166

167+
/**
168+
* Verify that the {@link jakarta.servlet.http.HttpServletResponse#sendError(int, String)} Servlet error message}
169+
* is equal to the given value.
170+
* @param errorMessage the expected Servlet error message (can be null)
171+
* @since 6.2.1
172+
*/
173+
public SELF hasErrorMessage(@Nullable String errorMessage) {
174+
new StringAssert(getResponse().getErrorMessage())
175+
.as("Servlet error message").isEqualTo(errorMessage);
176+
return this.myself;
177+
}
178+
166179
}

spring-test/src/test/java/org/springframework/test/web/servlet/assertj/AbstractMockHttpServletResponseAssertTests.java

+7
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ void hasRedirectedUrlWithWrongValue() {
110110
.withMessageContainingAll("Redirected URL", redirectedUrl, "another");
111111
}
112112

113+
@Test
114+
void hasServletErrorMessage() throws Exception{
115+
MockHttpServletResponse response = new MockHttpServletResponse();
116+
response.sendError(403, "expected error message");
117+
assertThat(fromResponse(response)).hasErrorMessage("expected error message");
118+
}
119+
113120

114121
private MockHttpServletResponse createResponse(String body) {
115122
try {

spring-test/src/test/java/org/springframework/test/web/servlet/assertj/MockMvcTesterIntegrationTests.java

+15
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import org.springframework.web.bind.annotation.RequestMapping;
6868
import org.springframework.web.bind.annotation.RequestParam;
6969
import org.springframework.web.bind.annotation.RequestPart;
70+
import org.springframework.web.bind.annotation.ResponseStatus;
7071
import org.springframework.web.bind.annotation.RestController;
7172
import org.springframework.web.bind.annotation.SessionAttributes;
7273
import org.springframework.web.context.WebApplicationContext;
@@ -596,6 +597,13 @@ void assertRedirectedUrlWithUnresolvedException() {
596597
result -> assertThat(result).hasRedirectedUrl("test"));
597598
}
598599

600+
@Test
601+
void assertErrorMessageWithUnresolvedException() {
602+
assertThatExceptionOfType(AssertionError.class)
603+
.isThrownBy(() -> assertThat(mvc.get().uri("/error/message")).hasErrorMessage("invalid"))
604+
.withMessageContainingAll("[Servlet error message]", "invalid", "expected error message");
605+
}
606+
599607
@Test
600608
void assertRequestWithUnresolvedException() {
601609
testAssertionFailureWithUnresolvableException(
@@ -798,6 +806,13 @@ public String two() {
798806
public String validation(@PathVariable @Size(max = 4) String id) {
799807
return "Hello " + id;
800808
}
809+
810+
@GetMapping("/error/message")
811+
@ResponseStatus(code = HttpStatus.BAD_REQUEST, reason = "expected error message")
812+
public void errorMessage() {
813+
814+
}
815+
801816
}
802817

803818
}

0 commit comments

Comments
 (0)