此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Framework 6.2.10spring-doc.cadn.net.cn

@WebAppConfiguration

@WebAppConfiguration是一个注解,可以应用于测试类以声明 该ApplicationContextloaded for an integration test 应该是WebApplicationContext.仅仅是@WebAppConfiguration在测试类上 确保WebApplicationContext加载以进行测试,使用默认值"file:src/main/webapp"对于 Web 应用程序根目录的路径(即 资源基础路径)。资源基础路径在后台用于创建MockServletContext,它充当ServletContext对于测试的WebApplicationContext.spring-doc.cadn.net.cn

以下示例演示如何使用@WebAppConfiguration注解:spring-doc.cadn.net.cn

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

要覆盖默认值,您可以使用 含蓄value属性。双classpath:file:资源前缀是 支持。如果未提供资源前缀,则假定路径为文件系统 资源。以下示例显示了如何指定类路径资源:spring-doc.cadn.net.cn

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

请注意@WebAppConfiguration必须与@ContextConfiguration,无论是在单个测试类中还是在测试类中 等级制度。请参阅@WebAppConfigurationjavadoc 了解更多详情。spring-doc.cadn.net.cn