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 question is related to
spring
junit
spring-test
applicationcontext
This works fine too:
@Autowired
ApplicationContext context;
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() {
}
}
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.
Source: Stackoverflow.com