假设我有一个名为FooInterceptor
的拦截器:
public class FooInterceptor extends HandlerInterceptorAdapter {
// ...
}
这是在上下文中配置的:
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="my.package.FooInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
我正在为一些控制器创建集成测试:
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "/context.xml")
@ActiveProfiles("test")
public class SomeControllerIT {
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@Before
public void setup() {
mockMvc = MockMvcBuilders
.webAppContextSetup(webApplicationContext)
.apply(springSecurity())
.build();
}
...
}
我尝试通过创建自定义配置来模拟它:
@Configuration
static class Config {
@Bean
@Primary
public FooInterceptor getFooInterceptor() {
return mock(FooInterceptor.class);
}
}
但是似乎没有起作用。实际的
FooInterceptor
仍然被产生并参与测试。如何正确地对其进行模拟?