上下文层次结构
在编写依赖于已加载 Spring ApplicationContext 的集成测试时,通常只需针对单个上下文进行测试即可。然而,有时针对 ApplicationContext 实例的层次结构进行测试是有益的,甚至是必要的。例如,如果您正在开发一个 Spring MVC Web 应用程序,您通常会有一个由 Spring 的 ContextLoaderListener 加载的根 WebApplicationContext,以及一个由 Spring 的 DispatcherServlet 加载的子 WebApplicationContext。这会产生一个父子上下文层次结构,其中共享组件和基础设施配置在根上下文中声明,并由 Web 特定组件在子上下文中消费。另一个用例可以在 Spring Batch 应用程序中找到,您通常有一个父上下文,为共享批处理基础设施提供配置,以及一个子上下文用于特定批处理作业的配置。
您可以通过在单个测试类或测试类层次结构中声明带有 @ContextHierarchy 注解的上下文配置来编写使用上下文层次结构的集成测试。如果在测试类层次结构中的多个类上声明了上下文层次结构,您还可以合并或覆盖上下文层次结构中特定命名级别的上下文配置。当合并给定层次结构级别的配置时,配置资源类型(即 XML 配置文件或组件类)必须保持一致。否则,上下文层次结构中的不同级别使用不同的资源类型进行配置是完全可以接受的。
|
如果您在上下文中作为上下文层次结构的一部分进行配置的测试中使用 有关更多详细信息,请参阅 Spring 测试注解 中对 |
本节中基于 JUnit Jupiter 的示例展示了需要使用上下文层次结构的集成测试的常见配置场景。
具有上下文层次结构的单个测试类
ControllerIntegrationTests 通过声明一个包含两个级别的上下文层次结构来表示 Spring MVC Web 应用程序的典型集成测试场景,一个用于根 WebApplicationContext(通过使用 TestAppConfig @Configuration 类加载),另一个用于调度程序 servlet WebApplicationContext(通过使用 WebConfig @Configuration 类加载)。自动装配到测试实例中的 WebApplicationContext 是子上下文的(即,层次结构中最低的上下文)。以下列表显示了此配置场景
-
Java
-
Kotlin
@ExtendWith(SpringExtension.class)
@WebAppConfiguration
@ContextHierarchy({
@ContextConfiguration(classes = TestAppConfig.class),
@ContextConfiguration(classes = WebConfig.class)
})
class ControllerIntegrationTests {
@Autowired
WebApplicationContext wac;
// ...
}
@ExtendWith(SpringExtension::class)
@WebAppConfiguration
@ContextHierarchy(
ContextConfiguration(classes = [TestAppConfig::class]),
ContextConfiguration(classes = [WebConfig::class]))
class ControllerIntegrationTests {
@Autowired
lateinit var wac: WebApplicationContext
// ...
}
具有隐式父上下文的类层次结构
此示例中的测试类在测试类层次结构中定义了一个上下文层次结构。AbstractWebTests 声明了 Spring 驱动的 Web 应用程序中根 WebApplicationContext 的配置。但是请注意,AbstractWebTests 没有声明 @ContextHierarchy。因此,AbstractWebTests 的子类可以选择参与上下文层次结构或遵循 @ContextConfiguration 的标准语义。SoapWebServiceTests 和 RestWebServiceTests 都扩展了 AbstractWebTests,并使用 @ContextHierarchy 定义了上下文层次结构。结果是加载了三个应用程序上下文(每个 @ContextConfiguration 声明一个),并且基于 AbstractWebTests 中的配置加载的应用程序上下文被设置为每个具体子类加载的上下文的父上下文。以下列表显示了此配置场景
-
Java
-
Kotlin
@ExtendWith(SpringExtension.class)
@WebAppConfiguration
@ContextConfiguration("file:src/main/webapp/WEB-INF/applicationContext.xml")
public abstract class AbstractWebTests {}
@ContextHierarchy(@ContextConfiguration("/spring/soap-ws-config.xml"))
public class SoapWebServiceTests extends AbstractWebTests {}
@ContextHierarchy(@ContextConfiguration("/spring/rest-ws-config.xml"))
public class RestWebServiceTests extends AbstractWebTests {}
@ExtendWith(SpringExtension::class)
@WebAppConfiguration
@ContextConfiguration("file:src/main/webapp/WEB-INF/applicationContext.xml")
abstract class AbstractWebTests
@ContextHierarchy(ContextConfiguration("/spring/soap-ws-config.xml"))
class SoapWebServiceTests : AbstractWebTests()
@ContextHierarchy(ContextConfiguration("/spring/rest-ws-config.xml"))
class RestWebServiceTests : AbstractWebTests()
具有合并上下文层次结构配置的类层次结构
此示例中的类展示了如何使用命名层次结构级别来合并上下文层次结构中特定级别的配置。BaseTests 定义了层次结构中的两个级别:parent 和 child。ExtendedTests 扩展了 BaseTests,并指示 Spring TestContext 框架合并 child 层次结构级别的上下文配置,通过确保 @ContextConfiguration 中 name 属性中声明的名称都是 child。结果是加载了三个应用程序上下文:一个用于 /app-config.xml,一个用于 /user-config.xml,一个用于 {"/user-config.xml", "/order-config.xml"}。与前面的示例一样,从 /app-config.xml 加载的应用程序上下文被设置为从 /user-config.xml 和 {"/user-config.xml", "/order-config.xml"} 加载的上下文的父上下文。以下列表显示了此配置场景
-
Java
-
Kotlin
@ExtendWith(SpringExtension.class)
@ContextHierarchy({
@ContextConfiguration(name = "parent", locations = "/app-config.xml"),
@ContextConfiguration(name = "child", locations = "/user-config.xml")
})
class BaseTests {}
@ContextHierarchy(
@ContextConfiguration(name = "child", locations = "/order-config.xml")
)
class ExtendedTests extends BaseTests {}
@ExtendWith(SpringExtension::class)
@ContextHierarchy(
ContextConfiguration(name = "parent", locations = ["/app-config.xml"]),
ContextConfiguration(name = "child", locations = ["/user-config.xml"]))
open class BaseTests {}
@ContextHierarchy(
ContextConfiguration(name = "child", locations = ["/order-config.xml"])
)
class ExtendedTests : BaseTests() {}
具有被覆盖上下文层次结构配置的类层次结构
与前一个示例相反,此示例演示了如何通过将 @ContextConfiguration 中的 inheritLocations 标志设置为 false 来覆盖上下文层次结构中给定命名级别的配置。因此,ExtendedTests 的应用程序上下文仅从 /test-user-config.xml 加载,并且其父上下文设置为从 /app-config.xml 加载的上下文。以下列表显示了此配置场景
-
Java
-
Kotlin
@ExtendWith(SpringExtension.class)
@ContextHierarchy({
@ContextConfiguration(name = "parent", locations = "/app-config.xml"),
@ContextConfiguration(name = "child", locations = "/user-config.xml")
})
class BaseTests {}
@ContextHierarchy(
@ContextConfiguration(
name = "child",
locations = "/test-user-config.xml",
inheritLocations = false
))
class ExtendedTests extends BaseTests {}
@ExtendWith(SpringExtension::class)
@ContextHierarchy(
ContextConfiguration(name = "parent", locations = ["/app-config.xml"]),
ContextConfiguration(name = "child", locations = ["/user-config.xml"]))
open class BaseTests {}
@ContextHierarchy(
ContextConfiguration(
name = "child",
locations = ["/test-user-config.xml"],
inheritLocations = false
))
class ExtendedTests : BaseTests() {}
具有 Bean 覆盖的上下文层次结构
当 @ContextHierarchy 与 Bean 覆盖(例如 @TestBean、@MockitoBean 或 @MockitoSpyBean)一起使用时,可能需要将覆盖应用于上下文层次结构中的单个级别。为了实现这一点,Bean 覆盖必须指定一个与通过 @ContextConfiguration 中的 name 属性配置的名称匹配的上下文名称。
以下测试类将第二个层次结构级别的名称配置为 "user-config",并同时指定 UserService 应该在名为 "user-config" 的上下文中封装在一个 Mockito spy 中。因此,Spring 将只尝试在 "user-config" 上文中创建 spy,而不会尝试在父上下文中创建 spy。
-
Java
-
Kotlin
@ExtendWith(SpringExtension.class)
@ContextHierarchy({
@ContextConfiguration(classes = AppConfig.class),
@ContextConfiguration(classes = UserConfig.class, name = "user-config")
})
class IntegrationTests {
@MockitoSpyBean(contextName = "user-config")
UserService userService;
// ...
}
@ExtendWith(SpringExtension::class)
@ContextHierarchy(
ContextConfiguration(classes = [AppConfig::class]),
ContextConfiguration(classes = [UserConfig::class], name = "user-config"))
class IntegrationTests {
@MockitoSpyBean(contextName = "user-config")
lateinit var userService: UserService
// ...
}
当在上下文层次结构的不同级别应用 Bean 覆盖时,您可能需要将所有 Bean 覆盖实例注入到测试类中才能与它们交互 — 例如,配置模拟的存根。但是,@Autowired 总是会注入在上下文层次结构最低级别找到的匹配 Bean。因此,要从上下文层次结构中的特定级别注入 Bean 覆盖实例,您需要使用适当的 Bean 覆盖注解注释字段并配置上下文级别的名称。
以下测试类将层次结构级别的名称配置为 "parent" 和 "child"。它还声明了两个 PropertyService 字段,这些字段配置为在各自的上下文中(名为 "parent" 和 "child")创建或替换 PropertyService Bean。因此,来自 "parent" 上下文的模拟将被注入到 propertyServiceInParent 字段中,来自 "child" 上下文的模拟将被注入到 propertyServiceInChild 字段中。
-
Java
-
Kotlin
@ExtendWith(SpringExtension.class)
@ContextHierarchy({
@ContextConfiguration(classes = ParentConfig.class, name = "parent"),
@ContextConfiguration(classes = ChildConfig.class, name = "child")
})
class IntegrationTests {
@MockitoBean(contextName = "parent")
PropertyService propertyServiceInParent;
@MockitoBean(contextName = "child")
PropertyService propertyServiceInChild;
// ...
}
@ExtendWith(SpringExtension::class)
@ContextHierarchy(
ContextConfiguration(classes = [ParentConfig::class], name = "parent"),
ContextConfiguration(classes = [ChildConfig::class], name = "child"))
class IntegrationTests {
@MockitoBean(contextName = "parent")
lateinit var propertyServiceInParent: PropertyService
@MockitoBean(contextName = "child")
lateinit var propertyServiceInChild: PropertyService
// ...
}