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
differences between 2 JUnit Assert classes
提问by Dónal
The JUnit framework contains 2 Assert
classes (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.Assert
and org.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.Assert
itself 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 Annotations
for 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 Assert
class. 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.junit
that follows better the normal conventions for package naming.
在重新设计时,他们还转向了新的包org.junit
,它更好地遵循包命名的正常约定。
回答by sblundy
I believe they are refactoring from junit.framework
to org.junit
and junit.framework.Assert
is maintained for backwards compatibility.
我相信他们正在从junit.framework
to重构,org.junit
并junit.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.Assert
and 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.Assert
will complain if you use the two-argument assertEquals()
with float
or double
, while junit.framework.Assert
will silently autobox it.
实际上有一个功能上的变化:org.junit.Assert
如果你使用两个参数assertEquals()
和float
or会抱怨double
,而junit.framework.Assert
会默默地自动装箱它。
回答by MatPag
In Android Studio (and so in IntelliJ too), you can globally exclude junit.framework
from auto-import proposal.
在 Android Studio 中(在 IntelliJ 中也是如此),您可以junit.framework
从自动导入提案中全局排除。
You can set the scope between IDE
or 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
首选项 -> 编辑器 -> 常规 -> 自动导入