What are the benefits of using @ContextHierarchy over pure @ContextConfiguration

Hi, I don’t understand what benefits the use of @ContextHierarchy gives me, as shown below:

@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 {
 ...
}

for using a single @ContextConfiguration with a location argument, as shown below:

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

In each case, application contexts are distributed between different junit test classes.

+4
source share
1 answer

The difference is that beans in each context within the context hierarchy cannot see beans in another context. This way you can isolate different parts of your subject under the test.

+2
source

Source: https://habr.com/ru/post/1675983/


All Articles