Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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(); 
like image 455
Vladimir Avatar asked Mar 11 '10 12:03

Vladimir


People also ask

How can you access the application context in Spring integration test?

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 .

Which of the following annotation is provided to load a Spring container in application JUnit?

@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.

What annotation causes JUnit to let Spring boot control the test?

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 .

What is the use of JUnit and spring annotations?

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.

What is @RunWith annotation in spring test?

@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

How do I pass a test In JUnit with spring?

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

What happened to the @RunWith annotation in JUnit?

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.


1 Answers

This works fine too:

@Autowired ApplicationContext context; 
like image 91
axtavt Avatar answered Nov 09 '22 16:11

axtavt