此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.2.0! |
上下文层次结构
当编写依赖于加载的 Spring 的集成测试时ApplicationContext
是的
通常足以针对单个上下文进行测试。但是,有时确实如此
对于针对ApplicationContext
实例。例如,如果您正在开发 Spring MVC Web 应用程序,则通常
有一个根WebApplicationContext
由 Spring 的ContextLoaderListener
以及
孩子WebApplicationContext
由 Spring 的DispatcherServlet
.这会导致
父子上下文层次结构,其中共享组件和基础设施配置
在根上下文中声明,并在子上下文中由 Web 特定的
组件。另一个用例可以在 Spring Batch 应用程序中找到,您经常在其中
具有为共享批处理基础架构提供配置的父上下文,以及
子上下文,用于特定批处理作业的配置。
您可以通过声明 context 来编写使用上下文层次结构的集成测试
配置与@ContextHierarchy
注解,或者在单个测试类上
或在测试类层次结构中。如果在多个类上声明了上下文层次结构
在 Test Class 层次结构中,您还可以合并或覆盖 Context Configuration
对于上下文层次结构中的特定命名级别。合并
层次结构中的给定级别,则配置资源类型(即 XML 配置
文件或组件类)必须一致。否则,完全可以接受
在使用不同的资源类型配置的上下文层次结构中具有不同的级别。
本节中其余基于 JUnit Jupiter 的示例显示了常见配置 需要使用上下文层次结构的集成测试的方案。
具有上下文层次结构的单个测试类
ControllerIntegrationTests
表示
Spring MVC Web 应用程序,通过声明一个由两个级别组成的上下文层次结构,
一个用于根WebApplicationContext
(使用TestAppConfig
@Configuration
类)和一个用于 Dispatcher 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
声明根的配置WebApplicationContext
在 Spring 支持的 Web 应用程序中。但请注意,AbstractWebTests
不声明@ContextHierarchy
.因此,的子类AbstractWebTests
可以选择参与上下文层次结构或遵循
的标准语义@ContextConfiguration
.SoapWebServiceTests
和RestWebServiceTests
两者都扩展AbstractWebTests
并通过以下方式定义上下文层次结构
用@ContextHierarchy
.结果是加载了三个应用程序上下文(一个
对于每个声明@ContextConfiguration
) 并加载应用程序上下文
根据AbstractWebTests
设置为每个
为 Concrete 子类加载的上下文。下面的清单显示了这一点
配置场景:
-
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()
具有合并上下文层次结构配置的类层次结构
此示例中的类演示了命名层次结构级别的使用,以便将
context 层次结构中特定级别的配置。BaseTests
定义两个级别
在层次结构中,parent
和child
.ExtendedTests
延伸BaseTests
并指示
Spring TestContext 框架来合并child
层次结构级别,方法是确保name
属性@ContextConfiguration
都是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() {}
具有覆盖上下文层次结构配置的类层次结构
与前面的示例相比,此示例演示了如何覆盖
配置,方法是将inheritLocations
标志输入@ContextConfiguration
自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() {}
在上下文层次结构中弄脏上下文 如果您使用@DirtiesContext 在上下文配置为
context 层次结构中,您可以使用hierarchyMode 标志来控制上下文缓存
已清除。有关更多详细信息,请参阅@DirtiesContext 在 Spring Testing Annotations 和@DirtiesContext javadoc 的 Java 文档。 |