上下文层次结构

当编写依赖于加载的 Spring 的集成测试时ApplicationContext是的 通常足以针对单个上下文进行测试。但是,有时确实如此 对于针对ApplicationContext实例。例如,如果您正在开发 Spring MVC Web 应用程序,则通常 有一个根WebApplicationContext由 Spring 的ContextLoaderListener以及 孩子WebApplicationContext由 Spring 的DispatcherServlet.这会导致 父子上下文层次结构,其中共享组件和基础设施配置 在根上下文中声明,并在子上下文中由 Web 特定的 组件。另一个用例可以在 Spring Batch 应用程序中找到,您经常在其中 具有为共享批处理基础架构提供配置的父上下文,以及 子上下文,用于特定批处理作业的配置。spring-doc.cadn.net.cn

您可以通过声明 context 来编写使用上下文层次结构的集成测试 配置与@ContextHierarchy注解,或者在单个测试类上 或在测试类层次结构中。如果在多个类上声明了上下文层次结构 在 Test Class 层次结构中,您还可以合并或覆盖 Context Configuration 对于上下文层次结构中的特定命名级别。合并 层次结构中的给定级别,则配置资源类型(即 XML 配置 文件或组件类)必须一致。否则,完全可以接受 在使用不同的资源类型配置的上下文层次结构中具有不同的级别。spring-doc.cadn.net.cn

本节中其余基于 JUnit Jupiter 的示例显示了常见配置 需要使用上下文层次结构的集成测试的方案。spring-doc.cadn.net.cn

具有上下文层次结构的单个测试类spring-doc.cadn.net.cn

ControllerIntegrationTests表示 Spring MVC Web 应用程序,通过声明一个由两个级别组成的上下文层次结构, 一个用于根WebApplicationContext(使用TestAppConfig @Configuration类)和一个用于 Dispatcher Servlet 的WebApplicationContext(使用WebConfig @Configuration类)。这WebApplicationContext)是子上下文的 (即 层次结构中最低的上下文)。下面的清单显示了此配置场景:spring-doc.cadn.net.cn

@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

	// ...
}

具有隐式父上下文的类层次结构spring-doc.cadn.net.cn

此示例中的测试类定义测试类中的上下文层次结构 等级制度。AbstractWebTests声明根的配置WebApplicationContext在 Spring 支持的 Web 应用程序中。但请注意,AbstractWebTests不声明@ContextHierarchy.因此,的子类AbstractWebTests可以选择参与上下文层次结构或遵循 的标准语义@ContextConfiguration.SoapWebServiceTestsRestWebServiceTests两者都扩展AbstractWebTests并通过以下方式定义上下文层次结构 用@ContextHierarchy.结果是加载了三个应用程序上下文(一个 对于每个声明@ContextConfiguration) 并加载应用程序上下文 根据AbstractWebTests设置为每个 为 Concrete 子类加载的上下文。下面的清单显示了这一点 配置场景:spring-doc.cadn.net.cn

@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()

具有合并上下文层次结构配置的类层次结构spring-doc.cadn.net.cn

此示例中的类演示了命名层次结构级别的使用,以便将 context 层次结构中特定级别的配置。BaseTests定义两个级别 在层次结构中,parentchild.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"}. 下面的清单显示了此配置场景:spring-doc.cadn.net.cn

@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() {}

具有覆盖上下文层次结构配置的类层次结构spring-doc.cadn.net.cn

与前面的示例相比,此示例演示了如何覆盖 配置,方法是将inheritLocations标志输入@ContextConfigurationfalse.因此, 的应用程序上下文ExtendedTests仅从/test-user-config.xml和 将其父项设置为从中加载的上下文/app-config.xml.以下清单 显示了此配置方案:spring-doc.cadn.net.cn

@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标志来控制上下文缓存 已清除。有关更多详细信息,请参阅@DirtiesContextSpring Testing Annotations@DirtiesContextjavadoc 的