Java 2 个 JUnit 断言类之间的差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/291003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:38:28  来源:igfitidea点击:

differences between 2 JUnit Assert classes

javaunit-testingjunitjunit4assert

提问by Dónal

The JUnit framework contains 2 Assertclasses (in different packages, obviously) and the methods on each appear to be very similar. Can anybody explain why this is?

JUnit 框架包含 2 个Assert类(显然在不同的包中)并且每个类的方法看起来非常相似。谁能解释一下这是为什么?

The classes I'm referring to are: junit.framework.Assertand org.junit.Assert.

我所指的类是:junit.framework.Assertorg.junit.Assert

采纳答案by Mnementh

The old method (of JUnit 3) was to mark the test-classes by extending junit.framework.TestCase. That inherited junit.framework.Assertitself and your test class gained the ability to call the assert methods this way.

旧方法(JUnit 3 的)是通过扩展junit.framework.TestCase. 它继承了junit.framework.Assert自身,并且您的测试类获得了以这种方式调用断言方法的能力。

Since version 4 of JUnit, the framework uses Annotationsfor marking tests. So you no longer need to extend TestCase. But that means, the assert methods aren't available. But you can make a static import of the new Assertclass. That's why all the assert methods in the new class are static methods. So you can import it this way:

从 JUnit 的第 4 版开始,该框架Annotations用于标记测试。所以你不再需要扩展TestCase. 但这意味着,断言方法不可用。但是您可以对新Assert类进行静态导入。这就是为什么新类中的所有断言方法都是静态方法的原因。所以你可以这样导入:

import static org.junit.Assert.*;

After this static import, you can use this methods without prefix.

在此静态导入后,您可以不使用前缀使用此方法。

At the redesign they also moved to the new package org.junitthat follows better the normal conventions for package naming.

在重新设计时,他们还转向了新的包org.junit,它更好地遵循包命名的正常约定。

回答by sblundy

I believe they are refactoring from junit.frameworkto org.junitand junit.framework.Assertis maintained for backwards compatibility.

我相信他们正在从junit.frameworkto重构,org.junitjunit.framework.Assert保持向后兼容性。

回答by ReneS

JUnit 3.X: junit.framework.Assert

JUnit 3.X: junit.framework.Assert

JUnit 4.X: org.junit.Assert

JUnit 4.X: org.junit.Assert

Prefer the newest one, especially when running JDK5 and higher with annotation support.

更喜欢最新的,尤其是在运行带有注释支持的 JDK5 及更高版本时。

回答by guerda

I did a rough source code compare and there are no serious changes. A lot of comments were added in org.junit.Assertand some refactorings are done. The only change is the comparison with Arrays. There are some code cleanups, but there's (imho) no functional change.

我做了一个粗略的源代码比较,没有严重的变化。添加了很多注释,org.junit.Assert并完成了一些重构。唯一的变化是与 的比较Arrays。有一些代码清理,但(恕我直言)没有功能变化

回答by David Moles

There is in fact a functional change: org.junit.Assertwill complain if you use the two-argument assertEquals()with floator double, while junit.framework.Assertwill silently autobox it.

实际上有一个功能上的变化:org.junit.Assert如果你使用两个参数assertEquals()floator会抱怨double,而junit.framework.Assert会默默地自动装箱它。

回答by MatPag

In Android Studio (and so in IntelliJ too), you can globally exclude junit.frameworkfrom auto-import proposal.

在 Android Studio 中(在 IntelliJ 中也是如此),您可以junit.framework从自动导入提案中全局排除。

You can set the scope between IDEor Project. If you don't have projects which use JUnit 3 you can safely stay with IDE scope.

您可以在IDE或之间设置范围Project。如果您没有使用 JUnit 3 的项目,您可以放心地使用 IDE 范围。

Setting position:

设置位置:

Preferences -> Editor -> General -> Auto Import

首选项 -> 编辑器 -> 常规 -> 自动导入

enter image description here

在此处输入图片说明