@WebAppConfiguration
@WebAppConfiguration
@WebAppConfiguration是一个注解,可以应用于测试类,用来声明用于集成测试的ApplicationContext应该是一个WebApplicationContext。只要在测试类上存在@WebAppConfiguration注解,就会确保为该测试加载一个WebApplicationContext,同时使用默认值“file:src/main/webapp”作为Web应用程序根目录(即资源基路径)的路径。这个资源基路径会在后台用于创建一个MockServletContext,该MockServletContext将作为测试所使用的WebApplicationContext的ServletContext。
以下示例展示了如何使用@WebAppConfiguration注释:
- Java
- Kotlin
@ContextConfiguration
@WebAppConfiguration 1
class WebAppTests {
// class body...
}
- \ [#1]
@WebAppConfiguration注解。
@ContextConfiguration
@WebAppConfiguration 1
class WebAppTests {
// class body...
}
- \ [#1]
@WebAppConfiguration注解。
要覆盖默认设置,可以使用隐式的 value 属性来指定不同的基础资源路径。classpath: 和 file: 两种资源前缀都受支持。如果没有提供资源前缀,则假设路径是文件系统资源。以下示例展示了如何指定类路径资源:
- Java
- Kotlin
@ContextConfiguration
@WebAppConfiguration("classpath:test-web-resources") 1
class WebAppTests {
// class body...
}
指定类路径资源。
@ContextConfiguration
@WebAppConfiguration("classpath:test-web-resources") 1
class WebAppTests {
// class body...
}
指定类路径资源。
请注意,@WebAppConfiguration 必须与 @ContextConfiguration 一起使用,可以在单个测试类中一起使用,也可以在测试类层次结构中一起使用。有关更多详细信息,请参阅 @WebAppConfiguration 的 Javadoc。