Using Spring-Test-MVC to unit test Spring-Security - Integrating the FilterChain / ServletFilter

So we're almost at our goal to test-drive our spring web-mvc application. We're using spring security to secure URLs or methods and we want to use Spring-Web-Mvc to test the controllers. The problem is: Spring security relies on a FilterChain defined in the web.xml:


While spring-web-mvc even with a custom context loader can only load the usual application context stuff:

@ContextConfiguration(loader = WebContextLoader.class, locations = {
        "file:src/main/webapp/WEB-INF/servlet-context.xml", "file:src/main/webapp/WEB-INF/dw-manager-context.xml" })
public class MvcTest {

    protected MockMvc mockMvc;

    public void loginRequiredTest() throws Exception {

        .andExpect(content().string(containsString("Please Login")));


As one can see, we set up our web app so that when calling /home an annonymus user will be prompted to log in. This is working fine with the web.xml in place but we have no idea how to integrate this into our tests.

Is there any way to add a filter to spring-test-mvc?

Right now I'm thinking I might have to start at the GenericWebContextLoader, dig into the MockRequestDipatcher which implements RequestDispatcher, add a filter there somehow and then tell the GenericWebContextLoader to use my implementation... But I'm quite unfamiliar with the whole web stack and would prefer an easier solution that helps me avoid digging into that stuff obviously...

Any ideas / solutions out there?

How would I add a filter by hand anyway? web.xml can't be the only place to do it...



Maybe you could mock the filter chain. There is a class in Spring for doing that. The code would look something like:

     MockHttpServletRequest request = new MockHttpServletRequest();

     MockHttpServletResponse response = new MockHttpServletResponse();
     MockFilterChain chain = new MockFilterChain(true);

     filterChainProxy.doFilter(request, response, chain);

Then you can add the org.springframework.web.filter.DelegatingFilterProxy instance to the chain in your code. Something like:

See also this forum thread.

Need Your Help

Refresh parent parent page after child closed

javascript php

to refresh parent page when children page is closed I usually use the following javascript code:

ios background images sizes and naming in xamarin

ios iphone xamarin.forms human-interface

I am using xamarin.forms to develop an app. The app has screens with background images. So to have proper background's i referred to the ios documentation here with taking dimensions of the launch/...