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

加载一个 WebApplicationContext

要指示TestContext框架加载WebApplicationContext而不是标准的ApplicationContext,你可以在相应的测试类上使用注解@WebAppConfigurationspring-doc.cadn.net.cn

The presence of @WebAppConfiguration on your test class instructs the TestContext framework (TCF) that a WebApplicationContext (WAC) should be loaded for your integration tests. In the background, the TCF makes sure that a MockServletContext is created and supplied to your test’s WAC. By default, the base resource path for your MockServletContext is set to src/main/webapp. This is interpreted as a path relative to the root of your JVM (normally the path to your project). If you are familiar with the directory structure of a web application in a Maven project, you know that src/main/webapp is the default location for the root of your WAR. If you need to override this default, you can provide an alternate path to the @WebAppConfiguration annotation (for example, @WebAppConfiguration("src/test/webapp")). If you wish to reference a base resource path from the classpath instead of the file system, you can use Spring’s classpath: prefix.spring-doc.cadn.net.cn

请注意,Spring 对 WebApplicationContext 实现的测试支持与其对标准 ApplicationContext 实现的支持相当。在使用 WebApplicationContext 进行测试时,您可以自由声明 XML 配置文件、Groovy 脚本或 @Configuration 类,通过使用 @ContextConfiguration。您还可以自由使用任何其他测试注解,例如 @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以及XML资源位置使用@ContextConfigurationspring-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