对于最新稳定版本,请使用 Spring Framework 7.0.6spring-doc.cadn.net.cn

加载一个 WebApplicationContext

要指示 TestContext 框架加载一个 WebApplicationContext 而不是标准的 ApplicationContext,您可以在相应的测试类上添加 @WebAppConfiguration 注解。spring-doc.cadn.net.cn

测试类中存在 @WebAppConfiguration 会指示测试上下文框架(TCF)为您的集成测试加载一个 WebApplicationContext(WAC)。在后台,TCF 确保创建了一个 MockServletContext 并将其提供给测试的 WAC。默认情况下,您的 MockServletContext 的基础资源路径设置为 src/main/webapp。这被解释为相对于 JVM 根目录的路径(通常是您的项目路径)。如果您熟悉 Maven 项目中 Web 应用程序的目录结构,您会知道 src/main/webapp 是 WAR 根目录的默认位置。如果需要覆盖此默认值,您可以为 @WebAppConfiguration 注解提供替代路径(例如 @WebAppConfiguration("src/test/webapp"))。如果您希望从类路径而不是文件系统引用基础资源路径,可以使用 Spring 的 classpath: 前缀。spring-doc.cadn.net.cn

请注意,Spring 对 WebApplicationContext 实现的测试支持与其对标准 ApplicationContext 实现的支持相当。在使用 WebApplicationContext 进行测试时,您可以通过 @ContextConfiguration 自由声明 XML 配置文件、Groovy 脚本或 @Configuration 类。您还可以自由使用任何其他测试注解,例如 @ActiveProfiles@TestExecutionListeners@Sql@Rollback 等。spring-doc.cadn.net.cn

本节其余的示例展示了一些用于加载 WebApplicationContext 的不同配置选项。以下示例展示了 TestContext 框架对“约定优于配置”原则的支持:spring-doc.cadn.net.cn

@ExtendWith(SpringExtension.class)

// defaults to "file:src/main/webapp"
@WebAppConfiguration

// detects "WacTests-context.xml" in the same package
// or static nested @Configuration classes
@ContextConfiguration
class WacTests {
	//...
}
@ExtendWith(SpringExtension::class)

// defaults to "file:src/main/webapp"
@WebAppConfiguration

// detects "WacTests-context.xml" in the same package
// or static nested @Configuration classes
@ContextConfiguration
class WacTests {
	//...
}

如果您使用 @WebAppConfiguration 注解测试类而未指定资源基础路径,则资源路径实际上默认为 file:src/main/webapp。类似地,如果您声明 @ContextConfiguration 但未指定资源 locations、组件 classes 或上下文 initializers,Spring 会尝试通过约定(即与 WacTests 类同一包中的 WacTests-context.xml 或静态嵌套 @Configuration 类)来检测您的配置是否存在。spring-doc.cadn.net.cn

以下示例展示了如何使用 @WebAppConfiguration 显式声明资源基础路径,并使用 @ContextConfiguration 声明 XML 资源位置:spring-doc.cadn.net.cn

@ExtendWith(SpringExtension.class)

// file system resource
@WebAppConfiguration("webapp")

// classpath resource
@ContextConfiguration("/spring/test-servlet-config.xml")
class WacTests {
	//...
}
@ExtendWith(SpringExtension::class)

// file system resource
@WebAppConfiguration("webapp")

// classpath resource
@ContextConfiguration("/spring/test-servlet-config.xml")
class WacTests {
	//...
}

这里需要注意的是,这两个注解在路径语义上的区别。@WebAppConfiguration 注解的资源路径默认基于文件系统,而 @ContextConfiguration 注解的资源位置则基于类路径。spring-doc.cadn.net.cn

以下示例展示了我们可以通过指定一个 Spring 资源前缀,来覆盖这两个注解的默认资源语义:spring-doc.cadn.net.cn

@ExtendWith(SpringExtension.class)

// classpath resource
@WebAppConfiguration("classpath:test-web-resources")

// file system resource
@ContextConfiguration("file:src/main/webapp/WEB-INF/servlet-config.xml")
class WacTests {
	//...
}
@ExtendWith(SpringExtension::class)

// classpath resource
@WebAppConfiguration("classpath:test-web-resources")

// file system resource
@ContextConfiguration("file:src/main/webapp/WEB-INF/servlet-config.xml")
class WacTests {
	//...
}

将本示例中的注释与前一个示例进行对比。spring-doc.cadn.net.cn