Support Servlet error message in MockMvc assertions

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
This commit is contained in:
Brian Clozel
2024-12-11 16:00:41 +01:00
parent 41d9f21ab9
commit 52006b71bc
3 changed files with 35 additions and 0 deletions

View File

@@ -22,6 +22,7 @@ import org.assertj.core.api.AbstractByteArrayAssert;
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;
@@ -163,4 +164,16 @@ public abstract class AbstractMockHttpServletResponseAssert<SELF extends Abstrac
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;
}
}