Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow customization of TestDispatcherServlet when using @MockMvcTest [SPR-14277] #18849

spring-projects-issues opened this issue May 17, 2016 · 3 comments


Copy link

@spring-projects-issues spring-projects-issues commented May 17, 2016

Mike Pleimann opened SPR-14277 and commented

I'm using spring-boot and attempting to test a @ControllerAdvice component with an exception handler method for NoHandlerFoundException. I've set spring.mvc.throw-exception-if-no-handler-found to true in application.yml which will set DispatcherServlet.throwExceptionIfNoHandlerFound true. When running the application as normal the DispatcherServlet throws the exceptions and they're handled appropriately. However, when attempting to test it with a @MockMvcTest, the NoHandlerFoundException is never thrown.

MockMvcBuilderSupport's constructor doesn't call setThrowExceptionIfNoHandlerFound on the TestDispatcherServlet as Boot's DispatcherServletAutoConfiguration#DispatcherServletConfiguration does when building the DispatcherServlet. Because it's passed to the MockMvc constructor without adding to the ApplicationContext and the TestDispatcherServlet isn't accessible there seems to be no way to set this property.

A Github issue was opened but closed as the change would be needed in spring-test rather than boot.

@WebMvcTest(controllers = { RootController.class, ExceptionHandlerAdvice.class })
public class RootControllerTest {
    private MockMvc mockMvc;

    public void testNoHandler() throws Exception {
                .andExpect(jsonPath("$.message").value(startsWith("Invalid Request")));
class ExceptionHandlerController {
    public ErrorData noHandlerFoundHandler(NoHandlerFoundException e, HttpServletRequest request) {
        String message = String.format("Invalid Request: %s", e.getMessage());;

        return new ErrorData(e.getClass(), message, request.getRequestURL().toString());

    public static class ErrorData {
        private final Instant timestamp =;
        private final Class<? extends Exception> type;
        private final String message;
        private final String requestUrl;

Affects: 4.3 RC2, 4.3 GA

Reference URL: spring-projects/spring-boot#5891

Referenced from: commits 30291a8, 1e3012c

0 votes, 5 watchers

Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Oct 28, 2016

Stéphane Nicoll commented

Rossen Stoyanchev I have updated the code to introduce DispatcherServletCustomizer. That way we can customize any property of DispatcherServlet via the builder. This would suits our needs in Boot. See snicoll@23294cf

We should probably deprecate the method that takes the boolean when we merge in 4.3.x. What do you think?

Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Oct 28, 2016

Rossen Stoyanchev commented

Stéphane Nicoll, it's worth the try. Sub-classes we don't know about (if any) probably extend from AbstractMockMvcBuilder.

Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Oct 29, 2016

Stéphane Nicoll commented

Alright this is fixed on master and merged in 4.3.x with an additional deprecated method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants