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();
This works fine too:
If your test class extends the Spring JUnit classes
(e.g.,
AbstractTransactionalJUnit4SpringContextTests
or any other class that extendsAbstractSpringContextTests
), you can access the app context by calling thegetContext()
method.Check out the javadocs for the package org.springframework.test.
It's possible to inject instance of
ApplicationContext
class by usingSpringClassRule
andSpringMethodRule
rules. It might be very handy if you would like to use another non-Spring runners. Here's an example:Since the tests will be instantiated like a Spring bean too, you just need to implement the ApplicationContextAware interface: