spring - 与纯@ContextConfiguration相比,使用@ContextHierarchy有什么优势?

标签 spring junit applicationcontext context-configuration

嗨,我不明白使用@ContextHierarchy的好处是什么,如下所示:

@ContextHierarchy({
  @ContextConfiguration("/test-db-setup-context.xml"),
  @ContextConfiguration("FirstTest-context.xml")
})
@RunWith(SpringJUnit4ClassRunner.class)
public class FirstTest {
 ...
}

@ContextHierarchy({
  @ContextConfiguration("/test-db-setup-context.xml"),
  @ContextConfiguration("SecondTest-context.xml")
})
@RunWith(SpringJUnit4ClassRunner.class)
public class SecondTest {
 ...
}

带有位置参数的单个@ContextConfiguration的使用情况,如下所示:
@ContextConfiguration(locations = {"classpath:test-db-setup-context.xml", "FirstTest-context.xml", "SecondTest-context.xml" })

在每种情况下,应用程序上下文在不同的junit测试类之间共享。

最佳答案

区别在于,上下文层次结构中每个上下文中的bean在另一个上下文中看不到bean。因此,您可以隔离待测项目的不同部分。

关于spring - 与纯@ContextConfiguration相比,使用@ContextHierarchy有什么优势?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43694807/

相关文章:

java - Jpa&Hibernate是否加载数据库中异步更改的数据?

java - 如何创建多部分响应

java - 在 Mockito mock 方法中将 JSONObject 作为参数

java - 如何在 Spring 中使用 applicationcontext.xml 文件访问属性文件字段?

java - 如何注入(inject) ApplicationContext 本身

java - 基于 XML 配置来连接带注释的类中的依赖关系?

java - IBM Redis 数据库 + Spring boot

java - 为什么 jUnit 的 fixtureSetup 必须是静态的?

java - greenmail 服务器收不到邮件

java - ApplicationContext 和 beans 检索