Skip to content

Commit

Permalink
Support Servlet error message in MockMvc assertions
Browse files Browse the repository at this point in the history
Prior to this commit, `MockMvc` would support checking for the Servlet
error message as the "response status reason". While this error message
can be driven with the `@ResponseStatus` annotation, this message is not
technically the HTTP status reason listed on the response status line.

This message is provided by the Servlet container in the error page when
the `response.sendError(int, String)` method is used.

This commit adds the missing
`mvc.get().uri("/error/message")).hasErrorMessage("error message")`
assertion to check for this Servlet error message.

Closes gh-34016
  • Loading branch information
bclozel committed Dec 11, 2024
1 parent 41d9f21 commit 52006b7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.assertj.core.api.AbstractStringAssert;
import org.assertj.core.api.Assertions;
import org.assertj.core.api.ByteArrayAssert;
import org.assertj.core.api.StringAssert;

import org.springframework.lang.Nullable;
import org.springframework.mock.web.MockHttpServletResponse;
Expand Down Expand Up @@ -163,4 +164,16 @@ public SELF hasRedirectedUrl(@Nullable String redirectedUrl) {
return this.myself;
}

/**
* Verify that the {@link jakarta.servlet.http.HttpServletResponse#sendError(int, String)} Servlet error message}
* is equal to the given value.
* @param errorMessage the expected Servlet error message (can be null)
* @since 6.2.1
*/
public SELF hasErrorMessage(@Nullable String errorMessage) {
new StringAssert(getResponse().getErrorMessage())
.as("Servlet error message").isEqualTo(errorMessage);
return this.myself;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ void hasRedirectedUrlWithWrongValue() {
.withMessageContainingAll("Redirected URL", redirectedUrl, "another");
}

@Test
void hasServletErrorMessage() throws Exception{
MockHttpServletResponse response = new MockHttpServletResponse();
response.sendError(403, "expected error message");
assertThat(fromResponse(response)).hasErrorMessage("expected error message");
}


private MockHttpServletResponse createResponse(String body) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.context.WebApplicationContext;
Expand Down Expand Up @@ -596,6 +597,13 @@ void assertRedirectedUrlWithUnresolvedException() {
result -> assertThat(result).hasRedirectedUrl("test"));
}

@Test
void assertErrorMessageWithUnresolvedException() {
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat(mvc.get().uri("/error/message")).hasErrorMessage("invalid"))
.withMessageContainingAll("[Servlet error message]", "invalid", "expected error message");
}

@Test
void assertRequestWithUnresolvedException() {
testAssertionFailureWithUnresolvableException(
Expand Down Expand Up @@ -798,6 +806,13 @@ public String two() {
public String validation(@PathVariable @Size(max = 4) String id) {
return "Hello " + id;
}

@GetMapping("/error/message")
@ResponseStatus(code = HttpStatus.BAD_REQUEST, reason = "expected error message")
public void errorMessage() {

}

}

}

0 comments on commit 52006b7

Please sign in to comment.