Loading a WebApplicationContext

为集成测试加载 WebApplicationContext 时,使用 @WebAppConfiguration 注解测试类。此注解指示 TestContext 框架使用 MockServletContext,该 MockServletContext 可以模仿应用程序的 Web 环境。默认情况下,@WebAppConfiguration 基础资源路径设置为 src/main/webapp,但可以通过该注解覆盖此设置。Spring 对 WebApplicationContext 测试的支持包括对 XML 配置文件、Groovy 脚本和 @Configuration 类的支持,以及对其他测试注解的自由使用。

为指示 TestContext framework 加载 WebApplicationContext 而不是标准的 ApplicationContext,你可以用 @WebAppConfiguration 为各自的测试类加注解。

To instruct the TestContext framework to load a WebApplicationContext instead of a standard ApplicationContext, you can annotate the respective test class with @WebAppConfiguration.

测试类中的 @WebAppConfiguration 表示 TestContext Framework (TCF) 应该为你的集成测试加载一个 WebApplicationContext (WAC)。在后台,TCF 会确保一个 MockServletContext 已创建,并已提供给测试的 WAC。默认情况下,MockServletContext 的基础资源路径设置为 src/main/webapp。这是一个相对于你的 JVM 根路径的路径(正常情况下是你的项目路径)。如果你熟悉 Maven 项目中 Web 应用程序的目录结构,你就会知道`src/main/webapp` 是用于 WAR 根默认位置的。如果你需要覆盖此默认设置,你可以为 @WebAppConfiguration 注解提供一个替换路径(例如,@WebAppConfiguration("src/test/webapp"))。如果你希望从类路径而不是文件系统引用一个基础资源路径,你可以使用 Spring 的 classpath: 前缀。

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 对 WebApplicationContext 实现的测试支持与它对标准 ApplicationContext 实现的支持相当。使用 WebApplicationContext 执行测试时,你可以使用 @ContextConfiguration 来自由地声明 XML 配置文件、Groovy 脚本或 @Configuration 类。你还可以自由地使用任何其他测试注解,如 @ActiveProfiles@TestExecutionListeners@Sql@Rollback 和其他注解。

Note that Spring’s testing support for WebApplicationContext implementations is on par with its support for standard ApplicationContext implementations. When testing with a WebApplicationContext, you are free to declare XML configuration files, Groovy scripts, or @Configuration classes by using @ContextConfiguration. You are also free to use any other test annotations, such as @ActiveProfiles, @TestExecutionListeners, @Sql, @Rollback, and others.

此部分中的其余示例展现了加载 WebApplicationContext 的一些不同配置选项。以下示例展示了 TestContext framework 对于配置惯例的支持:

The remaining examples in this section show some of the various configuration options for loading a WebApplicationContext. The following example shows the TestContext framework’s support for convention over configuration:

  • Conventions

  • Kotlin

@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 或 context initializers,Spring 会尝试使用惯例检测到你的配置存在(即,与 WacTests 类在同一个包之中的 WacTests-context.xml 或嵌套的静态 @Configuration 类)。

If you annotate a test class with @WebAppConfiguration without specifying a resource base path, the resource path effectively defaults to file:src/main/webapp. Similarly, if you declare @ContextConfiguration without specifying resource locations, component classes, or context initializers, Spring tries to detect the presence of your configuration by using conventions (that is, WacTests-context.xml in the same package as the WacTests class or static nested @Configuration classes).

以下示例演示了如何使用 @WebAppConfiguration 显式声明资源基路径,并使用 @ContextConfiguration 声明 XML 资源位置:

The following example shows how to explicitly declare a resource base path with @WebAppConfiguration and an XML resource location with @ContextConfiguration:

  • Default resource semantics

  • Kotlin

@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 资源位置基于类路径。

The important thing to note here is the different semantics for paths with these two annotations. By default, @WebAppConfiguration resource paths are file system based, whereas @ContextConfiguration resource locations are classpath based.

以下示例演示了我们可以通过指定 Spring 资源前缀来覆盖两者的默认资源语义:

The following example shows that we can override the default resource semantics for both annotations by specifying a Spring resource prefix:

  • Explicit resource semantics

  • Kotlin

@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 {
	//...
}

对比此示例中的注释与前一个示例。

Contrast the comments in this example with the previous example.