Josh Unger
Josh Unger

Reputation: 7153

Spring end to end test including filters and controller method using MockHttpServletRequest

I'm trying to write more of an end to end Spring test that will test my filters and the controller method associated to the given request. I tried RequestMappingHandlerAdapter.handle() but this doesn't call the filters. What classes do I need to use if I have a MockHttpServletRequest with a given path? I don't want to call doFilter followed by handle. Instead, I'm looking to call the code that ultimately calls both of these methods.

EDIT: needs to work in Spring 3.1.

Upvotes: 2

Views: 2118

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279880

You can register filters when you are setting up your MockMvc using MockMvcBuilders.

MockMvc mockMvc = MockMvcBuilders
          .webAppContextSetup(webApplicationContext)
          .addFilter(filter, urlPatterns);

The javadoc has more details. This method of registering filters is (mock-)equivalent to the web.xml configuration. For example

<filter-mapping>
    <filter-name>filter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Upvotes: 3

Related Questions