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

Vladimir picture Vladimir · Mar 11, 2010 · Viewed 115k times · Source

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

Answer

axtavt picture axtavt · Mar 11, 2010

This works fine too:

@Autowired
ApplicationContext context;