何时使用@RunWith 何时使用@ExtendWith
When to use @RunWith and when @ExtendWith
我和我的团队一直在使用 Spring 引导开发一堆微服务。
由于服务通过了 JUnit 和 Spring Boot 升级(我们现在使用 Spring Boot 2 和 JUnit 5),不同开发人员实现的不同 JUnit 现在使用不同的模式:
- @ExtendWith
- @RunWith
今天它们两者之间有什么区别?我们的单元测试真的需要它们还是嵌入在一些新的 Spring 引导注释中?
@RunWith
是 JUnit 4 中用于使用测试运行程序的旧注解。如果您使用的是 JUnit 5 (Jupiter),您应该使用 @ExtendWith
来使用 JUnit 扩展。
答案可以在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.class) as @SpringBootTest and the
other @…Testannotations are already annotated with it
.
如果您使用的 Junit 版本 < 5,那么您必须使用 @RunWith(SpringRunner.class)
或 @RunWith(MockitoJUnitRunner.class)
等
如果你使用的Junit version = 5,那么你必须使用@ExtendWith(SpringExtension.class)
或@ExtendWith(MockitoExtension.class)
等
我和我的团队一直在使用 Spring 引导开发一堆微服务。 由于服务通过了 JUnit 和 Spring Boot 升级(我们现在使用 Spring Boot 2 和 JUnit 5),不同开发人员实现的不同 JUnit 现在使用不同的模式:
- @ExtendWith
- @RunWith
今天它们两者之间有什么区别?我们的单元测试真的需要它们还是嵌入在一些新的 Spring 引导注释中?
@RunWith
是 JUnit 4 中用于使用测试运行程序的旧注解。如果您使用的是 JUnit 5 (Jupiter),您应该使用 @ExtendWith
来使用 JUnit 扩展。
答案可以在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.class) as @SpringBootTest and the other @…Testannotations are already annotated with it
.
如果您使用的 Junit 版本 < 5,那么您必须使用 @RunWith(SpringRunner.class)
或 @RunWith(MockitoJUnitRunner.class)
等
如果你使用的Junit version = 5,那么你必须使用@ExtendWith(SpringExtension.class)
或@ExtendWith(MockitoExtension.class)
等