Java JUnit 5批注的区别 [英] Java JUnit 5 annotations differences

查看:72
本文介绍了Java JUnit 5批注的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到根据《 JUnit 5用户指南》 引入了新的JUnit Jupiter .

I see there was introduced the new JUnit Jupiter according to JUnit 5 User Guide.

JUnit 5 = JUnit平台+ JUnit Jupiter + JUnit Vintage

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

我对在整个库中使用的同名注释感到困惑.两者之间有什么显着区别吗?

I am confused about the same-named annotations I use across the library. Is there any significant difference between these two ones?

  • org.junit.Test
  • org.junit.jupiter.api.Test
  • org.junit.Test
  • org.junit.jupiter.api.Test

上面链接页面中的描述对注释org.junit.jupiter.api.Test进行了如下解释:

The description from the linked page above explains the annotation org.junit.jupiter.api.Test as following:

表示方法是一种测试方法.与JUnit 4的@Test批注不同,该批注不声明任何属性,因为JUnit Jupiter中的测试扩展基于其自己的专用批注进行操作.除非重写这些方法,否则它们将被继承.

Denotes that a method is a test method. Unlike JUnit 4’s @Test annotation, this annotation does not declare any attributes, since test extensions in JUnit Jupiter operate based on their own dedicated annotations. Such methods are inherited unless they are overridden.

据我了解,主要区别在于新的批注属性被专用的批注和方法(例如assertTimeout(...))替换,与旧的@Test(timeout = 1000)不同.

As far as I understand, the main difference is the new annotation attributes are replaced with the dedicated annotations and methods (ex. assertTimeout(...)) unlike the old @Test(timeout = 1000).

文档讨论了来自JUnit 4的旧注释org.junit.Test,但是并没有明确解释版本JUnit 5中相同注释的目的,令我惊讶的是,它没有标记为@Deprecated-意味着在JUnit 5中仍然有使用此批注的目的,对吗?

The documentation speaks about the old annotation org.junit.Test from the JUnit 4, however, doesn't clearly explain the purpose of the same annotation in the version JUnit 5, which is for my surprise not marked as @Deprecated - it means there is still a purpose of using this annotation within JUnit 5, am I right?

我的问题是在JUnit 5中org.junit.Test的目的是什么,为什么不建议不使用它?我应该根据上述两个注释之间的选择是什么?

My question is what is the purpose of org.junit.Test in JUnit 5, why it's not deprecated and what my choice should be based on between the two annotations mentioned above.

推荐答案

org.junit.Test在JUnit5中不是不是,该类是由您对JUnit Vintage的依赖提供的.

org.junit.Test is not in JUnit5, that class is being provided by your dependency on JUnit Vintage.

JUnit Vintage包含JUnit Vintage测试引擎和诸如org.junit.Test之类的类,这使您可以将JUnit4测试与JUnit5测试一起运行.这是一种向后兼容的措施.

JUnit Vintage includes the JUnit Vintage test engine and classes such as org.junit.Test, this allows you to run JUnit4 tests alongside JUnit5 tests. It is a back compatability measure.

如果只想使用 JUnit5构造(并将JUnit4排除在项目之外),则只需放弃对JUnit Vintage的依赖,并专注于org.junit.jupiter.api.Test.

If you want to use only JUnit5 constructs (and leave JUnit4 out of your project) then just drop the dependency on JUnit Vintage and focus on org.junit.jupiter.api.Test.

如果您需要并排运行JUnit4和JUnit5测试(也许是在迁移/过渡期间),则保留对JUnit Vintage的依赖,但使用org.junit.jupiter.api.Test编写所有新的测试用例.

If you need to run JUnit4 and JUnit5 tests side by side (perhaps for the duration of a migraiton / cut-over period) then retain the dependency on JUnit Vintage but write all new test cases using org.junit.jupiter.api.Test.

更新:针对此问题...

Update: in response to this ...

我仍然不明白为什么不推荐使用org.junit.Test.

它在JUnit Jupiter中根本不存在(因此不赞成使用),在JUnit Vintage中也不建议使用,因为它是JUnit Vintage的核心元素.但是,我想我可以看到您来自哪里.您已升级到JUnit5,并且-令人困惑--org.junit.Test在类路径中仍可使用,而没有@Deprecated,因此没有明确指示其'请勿使用'状态.也许您应该考虑在运行JUnit5时是否需要/支持任何JUnit4构造.如果您不需要它,那么就不要包括对JUnit Vintage的依赖,就像org.junit.Test从不存在.

It simply does not exist in JUnit Jupiter (so deprecation is moot) and it is not deprecated in JUnit Vintage because it is a core element of JUnit Vintage. However, I think I can see where you are coming from; you upgraded to JUnit5 and - confusingly - org.junit.Test remains available on your classpath without @Deprecated so there is no clear indication of its 'do not use' status. Perhaps you should think about whether you want/need any JUnit4 constructs supported when running JUnit5. If you do not need that then just do not include a dependency on JUnit Vintage and it'll be like org.junit.Test never existed.

这篇关于Java JUnit 5批注的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆