Spring-Test-MVC / MockServletContext - content empty in test but working on Tomcat

后端 未结 3 2046
栀梦
栀梦 2021-01-12 05:43

We\'re trying to set up Spring-Test-MVC for our Spring-MVC web app. We started out using freemarker and everything was fine. We decided against it though and are now trying

3条回答
  •  感情败类
    2021-01-12 06:03

    I have created a modified version of MockRequestDispatcher that builds the same chain as MockMvc and passes the request to that chain for forward requests. That fixes this issue for me. Some more work has to be done if the view is rendered outside of the dispatcher servlet (e.g. via jsp).

    The code is there: https://gist.github.com/micw/a8b20720db900762a29d

    Most work was to inject it at the right place. I did this my creating a RequestPostProcessor for MockMvc and some mockito-magic that intercepts the MockHttpServletRequest.getRequestDispatcher call.

    To use it, add it to your test classpath and call it directly after creating your MockMvc instance:

        mvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
                [...]
                .build();
        WebMvcRequestDispatcherForwardFix.apply(mvc);
    

提交回复
热议问题