Vladimir
Vladimir

Reputation: 13153

How to access Spring context in jUnit tests annotated with @RunWith and @ContextConfiguration?

I have following test class

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/services-test-config.xml"})
public class MySericeTest {

  @Autowired
  MyService service;
...

}

Is it possible to access services-test-config.xml programmatically in one of such methods? Like:

ApplicationContext ctx = somehowGetContext();

Upvotes: 70

Views: 168466

Answers (5)

Sudeep Krishnan M
Sudeep Krishnan M

Reputation: 141

Created one post related to this issue, this really helpful to gain confidence

Try this link for more details

@Test
void TestBeanLaunch()
{
    context.run(it -> {
        /*
         * I can use assertThat to assert on the context
         * and check if the @Bean configured is present
         * (and unique)
         */
        assertThat(it).hasSingleBean(PracticeApplication.class);
        assertThat(it).hasSingleBean(PracticeApplication.TestBean.class);
        assertThat(it.getBean("BeanTest")).isInstanceOf(PracticeApplication.TestBean.class);
    });
} 

Upvotes: -1

Daff
Daff

Reputation: 44205

Since the tests will be instantiated like a Spring bean too, you just need to implement the ApplicationContextAware interface:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/services-test-config.xml"})
public class MySericeTest implements ApplicationContextAware
{

  @Autowired
  MyService service;
...
    @Override
    public void setApplicationContext(ApplicationContext context)
            throws BeansException
    {
        // Do something with the context here
    }
}

For non xml needs, you can also do this:

@RunWith(SpringJUnit4ClassRunner.class)
/* must provide some "root" for the app-context, use unit-test file name to the context is empty */
@ContextConfiguration(classes = MyUnitTestClass.class)
public class MyUnitTestClass implements ApplicationContextAware {

Upvotes: 64

Laplas
Laplas

Reputation: 771

It's possible to inject instance of ApplicationContext class by using SpringClassRule and SpringMethodRule rules. It might be very handy if you would like to use another non-Spring runners. Here's an example:

@ContextConfiguration(classes = BeanConfiguration.class)
public static class SpringRuleUsage {

    @ClassRule
    public static final SpringClassRule springClassRule = new SpringClassRule();

    @Rule
    public final SpringMethodRule springMethodRule = new SpringMethodRule();

    @Autowired
    private ApplicationContext context;

    @Test
    public void shouldInjectContext() {
    }
}

Upvotes: 3

duffymo
duffymo

Reputation: 308733

If your test class extends the Spring JUnit classes
(e.g., AbstractTransactionalJUnit4SpringContextTests or any other class that extends AbstractSpringContextTests), you can access the app context by calling the getContext() method.
Check out the javadocs for the package org.springframework.test.

Upvotes: 9

axtavt
axtavt

Reputation: 242686

This works fine too:

@Autowired
ApplicationContext context;

Upvotes: 97

Related Questions