When to use @RunWith and when @ExtendWith

試著忘記壹切 提交于 2020-01-13 07:42:05

问题


My team and I have been working on a bunch of microservices using Spring boot. Since the services went through JUnit and Spring Boot upgrades (We're using now Spring Boot 2 and JUnit 5), different JUnit implemented by different devs, are now using different patterns with:

  • @ExtendWith
  • @RunWith

Today what's the difference between the two of them and do we really need them for our Unit Tests or are embedded in some new Spring Boot annotation?


回答1:


If you are using Junit version < 5, so you have to use @RunWith(SpringRunner.class) or @RunWith(MockitoJUnitRunner.class) etc.

If you are using Junit version = 5, so you have to use @ExtendWith(SpringExtension.class) or @ExtendWith(MockitoExtension.class) etc.

  1. SpringRunner
  2. MockitoJUnitRunner
  3. SpringExtension
  4. MockitoExtension



回答2:


The answer can be found in the documentation:

If you are using JUnit 4, don’t forget to 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) as @SpringBootTest and the other @…Testannotations are already annotated with it

.




回答3:


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



来源:https://stackoverflow.com/questions/55276555/when-to-use-runwith-and-when-extendwith

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