What does this annotation do?
When would I want to use it?
When would I not want to use it?
@RunWith(SpringJUnit4ClassRunner.class)
I can find more usages of this when I Google and do not find a 101 explanation as to what this annotation is supposed to communicate to me or when/why I would use it?
@RunWith(SpringRunner. class) provides a bridge between Spring Boot test features and JUnit. Whenever we are using any Spring Boot testing features in our JUnit tests, this annotation will be required.
When a class is annotated with @RunWith or extends a class annotated with it, JUnit will invoke the class that it references to run the tests in that class instead of the runner built with JUnit.
If a JUnit class or its parent class is annotated with @RunWith, JUnit framework invokes the specified class as a test runner instead of running the default runner. The specified 'value' element must be a subclass of the abstract org.
There is no difference, from the javadoc: SpringRunner is an alias for the SpringJUnit4ClassRunner.
The annotation is used to configure a unit test that required Spring's dependency injection.
From Spring Reference - 10. Unit Testing:
10.1 Creating a Unit Test Class
In order for the unit test to run a batch job, the framework must load the job's ApplicationContext. Two annotations are used to trigger this:
@RunWith(SpringJUnit4ClassRunner.class): Indicates that the class should use Spring's JUnit facilities.
@ContextConfiguration(locations = {...}): Indicates which XML files contain the ApplicationContext.
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