8

I am implementing a HandlerInterceptor that needs to execute business logic prior/after requests to several paths are handled. I want to test the execution of the Interceptor by mocking the request handle lifecycle.

Here is how the interceptor is registered:

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/test*"/>
        <bean class="x.y.z.TestInterceptor" />
    </mvc:interceptor>
</mvc:interceptors>

I want to test not only the preHandle and postHandle methods, but the mapping to the path as well.

kpentchev
  • 3,040
  • 2
  • 24
  • 41

2 Answers2

17

The following test can be written with the help of JUnit and spring-test:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:META-INF/spring.xml", ... })
public class InterceptorTest {

@Autowired
private RequestMappingHandlerAdapter handlerAdapter;

@Autowired
private RequestMappingHandlerMapping handlerMapping;

@Test
public void testInterceptor() throws Exception{


    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRequestURI("/test");
    request.setMethod("GET");


    MockHttpServletResponse response = new MockHttpServletResponse();

    HandlerExecutionChain handlerExecutionChain = handlerMapping.getHandler(request);

    HandlerInterceptor[] interceptors = handlerExecutionChain.getInterceptors();

    for(HandlerInterceptor interceptor : interceptors){
        interceptor.preHandle(request, response, handlerExecutionChain.getHandler());
    }

    ModelAndView mav = handlerAdapter. handle(request, response, handlerExecutionChain.getHandler());

    for(HandlerInterceptor interceptor : interceptors){
        interceptor.postHandle(request, response, handlerExecutionChain.getHandler(), mav);
    }

    assertEquals(200, response.getStatus());
    //assert the success of your interceptor

}

HandlerExecutionChain is populated with all the mapped interceptors for the specific request. If the mapping is failing, the interceptor will not be present in the list and hence not executed and the assertion at the end will fail.

kpentchev
  • 3,040
  • 2
  • 24
  • 41
6

The Spring Framework provides dedicated support for testing your Spring MVC configuration and components in the form of the Spring MVC Test Framework, available since Spring Framework 3.2 (and as a separate spring-test-mvc project since Spring Framework 3.1).

In the first link you will find numerous examples of how to use the Spring MVC Test framework. In addition, you will also find slides by Rossen Stoyanchev (author of the Spring MVC Test Framework) in our Spring 3.1 and MVC Testing Support and Testing Web Apps with Spring Framework 3.2 presentations from SpringOne 2GX.

I'm confident those resources should help you get your tests written concisely, but if you still need help, feel free to post back here.

Regards,

Sam (author of the Spring TestContext Framework)

Sam Brannen
  • 29,611
  • 5
  • 104
  • 136
  • Thanks for pointing me to this mvc test framework! I was used to mocking the mvc myself from the 3.1 times... Should make testing much more consistent! – kpentchev Jun 12 '14 at 11:57