@WebAppConfiguration

@WebAppConfiguration 是一个类级注解,您可以使用它来声明为集成测试加载的 ApplicationContext 应该是 WebApplicationContext。 测试类上仅存在 @WebAppConfiguration 即可确保为测试加载 WebApplicationContext,使用默认值 "file:src/main/webapp" 作为 Web 应用程序根目录的路径(即资源基础路径)。 资源基础路径在后台用于创建 MockContextImpl,它充当测试的 WebApplicationContextMockContextImpl

以下示例显示了如何使用 @WebAppConfiguration 注解:

  • Java

@ContextConfiguration
@WebAppConfiguration (1)
class WebAppTests {
  // 类体...
}
1 @WebAppConfiguration 注解。

要覆盖默认值,您可以使用隐式 value 属性指定不同的基础资源路径。 支持 classpath:file: 资源前缀。 如果未提供资源前缀,则假定路径是文件系统资源。 以下示例显示了如何指定类路径资源:

  • Java

@ContextConfiguration
@WebAppConfiguration("classpath:test-web-resources") (1)
class WebAppTests {
  // 类体...
}
1 指定类路径资源。

请注意,@WebAppConfiguration 必须与 @ContextConfiguration 结合使用,无论是在单个测试类中还是在测试类层次结构中。 有关更多详细信息,请参阅 @WebAppConfiguration javadoc。