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(); 回答1: Since the tests will be instantiated like a Spring bean too, you just need to implement the ApplicationContextAware interface: @RunWith