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();
By default the ApplicationContext is loaded using the GenericXmlContextLoader which loads a context from XML Spring configuration files. You can then access beans from the ApplicationContext by annotating fields in your test class with @Autowired , @Resource , or @Inject .
@RunWith. When a class is annotated with @RunWith or extends a class annotated with @RunWith, JUnit will invoke the class it references to run the tests in that class instead of the runner built into JUnit. Let us configure jUnit to use Spring jUnit Class runner. @RunWith(SpringJUnit4ClassRunner.
Spring Boot provides a @SpringBootTest annotation, which can be used as an alternative to the standard spring-test @ContextConfiguration annotation when you need Spring Boot features. The annotation works by creating the ApplicationContext used in your tests through SpringApplication .
I allow the Spring Framework to manage the dependency injection. To support Spring and JUnit, we need to add two new annotations to our test class. The first annotation is . The annotation is provided by the JUnit team, which is an API to allow for the extension of JUnit to allow for a customized test runner class.
@RunWith – is an annotation to tag a class that this should Run with a specific runner class. There are a lot of alternative runners for JUnit but since we are using Spring test, we use: SpringJUnit4ClassRunner.class
To support Spring and JUnit, we need to add two new annotations to our test class. The first annotation is . The annotation is provided by the JUnit team, which is an API to allow for the extension of JUnit to allow for a customized test runner class. Within this annotation, we’re passing in the class
In JUnit 5, the @RunWith annotation has been replaced by the more powerful @ExtendWith annotation. However, the @RunWith annotation can still be used in JUnit5 for the sake of the backward compatibility.
This works fine too:
@Autowired ApplicationContext context;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With