Java JUnit 5 注释差异

Java JUnit 5 annotations differences

我看到根据 JUnit 5 User Guide 介绍了新的 JUnit Jupiter。

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

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

上面链接页面的描述对注释 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(timeout = 1000) 不同。

文档谈到了 JUnit 4 中的旧注解 org.junit.Test,但是,并没有清楚地解释 JUnit 5 版本中相同注解的用途,令我惊讶的是它没有标记为 @Deprecated - 这意味着在 JUnit 5 中仍然有使用此注释的目的,对吗?

我的问题是 org.junit.Test 在 JUnit 5 中的用途是什么,为什么它没有被弃用,以及我的选择应该基于上面提到的两个注释。

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

JUnit Vintage 包括 JUnit Vintage 测试引擎和 classes,例如 org.junit.Test,这允许您 运行 JUnit4 测试和 JUnit5 测试。这是一种后向兼容性措施。

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

如果您需要 运行 JUnit4 和 JUnit5 并排测试(可能在迁移期间/cut-over 期间),则保留对 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 的 class 路径上仍然可用,因此没有明确指示其 ' 不要使用 ' 状态。也许您应该考虑 want/need 在 运行 宁 JUnit5 时是否支持任何 JUnit4 构造。如果您不需要它,那么就不要包含对 JUnit Vintage 的依赖,它就像 org.junit.Test 从未存在过一样。