Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this do: @RunWith(SpringJUnit4ClassRunner.class)

Tags:

junit

spring

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?

like image 583
Jay Vee Avatar asked Aug 14 '14 20:08

Jay Vee


People also ask

What is the use of @RunWith SpringRunner class?

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

What is the use of @RunWith annotation?

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.

What is the use of @RunWith in 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.

What is the difference between SpringJUnit4ClassRunner and SpringRunner?

There is no difference, from the javadoc: SpringRunner is an alias for the SpringJUnit4ClassRunner.


1 Answers

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.

like image 64
Ricardo Veguilla Avatar answered Oct 08 '22 22:10

Ricardo Veguilla