使用 Web Mocks
为了提供全面的 Web 测试支持,TestContext 框架具有默认启用的 WebMockTestExecutionListener。
当针对 GenericMockWebApplicationContext 进行测试时,此 TestExecutionListener
在每个测试方法之前使用 Infra Web 的 HttpContextHolder 设置默认的线程本地状态 创建 MockRequest、MockResponse 和 MockHttpContext。
WebMockTestExecutionListener 还确保可以将
MockResponse 和 MockHttpContext 注入到测试实例中,并且一旦测试完成,它就会清理线程本地状态。
一旦为测试加载了 GenericMockWebApplicationContext,你可能会发现你需要与 Web mock 进行交互——例如,在调用 Web 组件之后设置测试夹具或执行断言。
以下示例显示了哪些 mock 可以自动装配到测试实例中。请注意,GenericMockWebApplicationContext 在整个测试套件中都是缓存的,而其他 mock 则由
WebMockTestExecutionListener 针对每个测试方法进行管理。
-
Injecting mocks
@JUnitWebConfig
class WacTests {
@Autowired
ApplicationContext wac; // 缓存的
@Autowired
Session session;
@Autowired
HttpContext request;
//...
}