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

@WebAppConfiguration

@WebAppConfiguration是一个类级注解,可用于声明ApplicationContextloaded for an integration test 应为WebApplicationContext. 仅仅存在@WebAppConfiguration在测试类上确保WebApplicationContext为测试加载,使用默认值"file:src/main/webapp"对于 Web 应用程序根目录的路径(即 resource base path) 的 Sample。资源基路径在后台用于创建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:资源前缀为 支持。如果未提供资源前缀,则假定该路径为文件系统 资源。下面的示例展示了如何指定 Classpath 资源:spring-doc.cadn.net.cn

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

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