SpringRunner vs SpringBootTest

We Are Going To Discuss About SpringRunner vs SpringBootTest. So lets Start this Java Article.

SpringRunner vs SpringBootTest

  1. SpringRunner vs SpringBootTest

    @RunWith(SpringRunner.class) : You need this annotation to just enable spring boot features like @Autowire, @MockBean etc.. during junit testing

  2. SpringRunner vs SpringBootTest

    @RunWith(SpringRunner.class) : You need this annotation to just enable spring boot features like @Autowire, @MockBean etc.. during junit testing

Solution 1

@RunWith(SpringRunner.class) : You need this annotation to just enable spring boot features like @Autowire, @MockBean etc.. during junit testing

is used to provide 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.

@SpringBootTest : This annotation is used to load complete application context for end to end integration testing

The @SpringBootTest annotation can be used when we need to bootstrap the entire container. The annotation works by creating the ApplicationContext that will be utilized in our tests.

Here is the article with clear examples on both scenarios Baeldung

Original Author Deadpool Of This Content

Solution 2

@RunWith is an old annotation from JUnit 4 to use test runners. If you’re using JUnit 5 (Jupiter), you should use @ExtendWith to use JUnit extensions

See
https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-testing

“If you are using JUnit 4, don’t forget to also add @RunWith(SpringRunner.class) to your test, otherwise the annotations will be ignored. If you are using JUnit 5, there’s no need to add the equivalent @ExtendWith(SpringExtension.class) as @SpringBootTest and the other @…Test annotations are already annotated with it.

Original Author Tore Gard at techpros no Of This Content

Solution 3

From spring.io :

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

@SpringBootTest is saying “bootstrap with Spring Boot’s support” (e.g.
load application.properties and give me all the Spring Boot goodness)

So if you don’t need everything that Spring Boot loads for your integration test, you may not need @SpringBootTest

Original Author Xavier FRANCOIS Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment