【发布时间】:2018-03-25 10:24:50
【问题描述】:
说我有一些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 仍会生成并参与测试。
如何正确地模拟它?
【问题讨论】:
标签: java spring mocking integration-testing interceptor