我看到根据 JUnit 5 User Guide 引入了新的 JUnit Jupiter .
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
我对我在整个库中使用的同名注释感到困惑。这两者之间有什么显着差异吗?
org.junit.Test
org.junit.jupiter.api.Test
上面链接页面的描述解释了注释 org.junit.jupiter.api.Test
如下:
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 不同(超时 = 1000)
。
文档谈到了 JUnit 4 中的旧注解 org.junit.Test
,但是,并没有清楚地解释 JUnit 5 版本中相同注解的用途,这是为了我令人惊讶的是未标记为 @Deprecated
- 这意味着在 JUnit 5 中仍然有使用此注释的目的,对吗?
我的问题是 JUnit 5 中 org.junit.Test
的目的是什么,为什么它没有被弃用,以及我的选择应该基于上面提到的两个注释。
最佳答案
org.junit.Test
在 JUnit5 中不,该类由您对 JUnit Vintage 的依赖项提供。
JUnit Vintage 包括 JUnit Vintage 测试引擎和类,例如 org.junit.Test
,这使您可以同时运行 JUnit4 测试和 JUnit5 测试。这是一种反向兼容性措施。
如果您想仅使用JUnit5 构造(并将JUnit4 排除在您的项目之外),那么只需放弃对JUnit Vintage 的依赖并专注于org.junit.jupiter.api.Test
。
如果您需要并排运行 JUnit4 和 JUnit5 测试(可能在迁移/切换期间),则保留对 JUnit Vintage 的依赖性,但使用 org.junit 编写所有新测试用例。 jupiter.api.Test
.
更新:回应这个...
I still don't understand why
org.junit.Test
isn't deprecated.
它在 JUnit Jupiter 中根本不存在(因此弃用没有实际意义)并且在 JUnit Vintage 中也没有弃用,因为它是 JUnit Vintage 的核心元素。但是,我想我可以看到你来自哪里;您升级到 JUnit5 并且 - 令人困惑 - org.junit.Test
在没有 @Deprecated
的情况下仍然在您的类路径上可用,因此没有明确指示其“不要使用”状态。也许您应该考虑在运行 JUnit5 时是否想要/需要任何支持的 JUnit4 构造。如果您不需要它,那么就不要包含对 JUnit Vintage 的依赖,它就像 org.junit.Test
从未存在过一样。
关于Java JUnit 5 注释差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48853312/