Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between SpringJUnit4ClassRunner and SpringRunner

Whenever I see a blog post related to Spring testing I see either of these classes but do not understand the real difference:

@RunWith(SpringRunner.class)
@RunWith(SpringJUnit4ClassRunner.class)
like image 528
Humoyun Ahmad Avatar asked Nov 23 '17 02:11

Humoyun Ahmad


People also ask

What is a SpringRunner?

SpringRunner is an alias for the SpringJUnit4ClassRunner , which joins JUnit testing library with the Spring TestContext Framework. We use it with @RunWith(SpringRunner. class) . With SpringRunner , we can implement standard JUnit 4-based unit and integration tests.

What does @RunWith SpringRunner class mean?

@RunWith(SpringRunner. class) tells JUnit to run using Spring's testing support. SpringRunner is the new name for SpringJUnit4ClassRunner , it's just a bit easier on the eye.

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


2 Answers

There is no difference, from the javadoc:

SpringRunner is an alias for the SpringJUnit4ClassRunner.

ref: https://docs.spring.io/spring/docs/4.3.0.RC2_to_4.3.0.RELEASE/Spring%20Framework%204.3.0.RELEASE/org/springframework/test/context/junit4/SpringRunner.html

like image 174
StvnBrkdll Avatar answered Oct 14 '22 00:10

StvnBrkdll


@RunWith(SpringRunner.class) tells JUnit to run using Spring’s testing support. SpringRunner is the new name for SpringJUnit4ClassRunner, it’s just a bit easier on the eye.

SpringRunner is only available on spring-test 4.3.

SpringRunner class extends SpringJUnit4ClassRunner.

Source code of SpringRunner is

package org.springframework.test.context.junit4;

import org.junit.runners.model.InitializationError;

public final class SpringRunner extends SpringJUnit4ClassRunner {

    public SpringRunner(Class<?> clazz) throws InitializationError {
        super(clazz);
    }

}
like image 20
Joby Wilson Mathews Avatar answered Oct 14 '22 02:10

Joby Wilson Mathews