嗨,我不明白使用@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/