SpringRunner vs SpringBootTest

别来无恙 提交于 2020-02-01 05:08:25

问题


In unit test, what are the differences between @Runwith(SpringRunner.class) & @SpringBootTest?

Can you explain to me the use cases of each one?


回答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




回答2:


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



来源:https://stackoverflow.com/questions/58901288/springrunner-vs-springboottest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!