70

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();
1
  • I'm pretty sure it is possible even with the vanilla runner JUnit4.class, which the answers are ignoring. I need to Google around a bit more. Commented May 28, 2019 at 18:01

5 Answers 5

97

This works fine too:

@Autowired
ApplicationContext context;
Sign up to request clarification or add additional context in comments.

3 Comments

Confirmed, in Spring Integration 3.0.2. Neatest solution.
However this solution is viable only if you are using it into a managed bean. By implementing ApplicationContextAware is working well even in a POJO
Works fine on spring 4.2
64

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 {

1 Comment

Not possible in Spring 3.0 and up, up to this date.
9

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.

1 Comment

Is there any difference when using the AbstractTransactionalJUnit4SpringContextTests, or AbstractSpringContextTests vs simply extending ApplicationContextAware in my junit test? What benefits are provided by the 2 abstract test classes suggested in this answer?
3

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() {
    }
}

Comments

-1

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);
    });
} 

1 Comment

Link in answer is dead - "DNS_PROBE_FINISHED_NXDOMAIN".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.