与纯@ContextConfiguration相比,使用@ContextHierarchy有什么好处

Que*_*ker 9 junit spring 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 {
 ...
}
Run Code Online (Sandbox Code Playgroud)

过度使用带有locations参数的单个@ContextConfiguration,如下所示:

@ContextConfiguration(locations = {"classpath:test-db-setup-context.xml", "FirstTest-context.xml", "SecondTest-context.xml" })
Run Code Online (Sandbox Code Playgroud)

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

Guy*_*Guy 2

不同之处在于,上下文层次结构中每个上下文中的 Bean 无法看到其他上下文中的 Bean。因此您可以隔离被测项目的不同部分。

  • 子上下文中的 Bean 不正确,可以看到父上下文中的 Bean,但其他情况则不然 (6认同)