java 单元测试:断言不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10039079/
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
Unit test: assert not work?
提问by Hoàng Long
I'm applying UnitTest just for a while, and today I met something very strange. Considering the following code:
我刚刚应用了 UnitTest 一段时间,今天我遇到了一些非常奇怪的事情。考虑以下代码:
TestObject alo = null;
assert alo != null; // Pass!!!
Assert.assertNotNull(alo); // Fail, as expected.
I googled around and find that assert is java-built-in, while assertNotNull is JUnit supported. But I can't understand why assert don't complain anything about the null object?
我搜索了一下,发现 assert 是 Java 内置的,而 assertNotNull 是 JUnit 支持的。但我不明白为什么断言不抱怨空对象?
回答by Ryan Nelson
Hoang, I think you're getting a little confused between Java language asserts and JUnit asserts.
Hoang,我认为您在 Java 语言断言和 JUnit 断言之间有点混淆。
The assertkeyword in Java was added in 1.4, and intended to allow to verify internal consistency in a class. The best practice recommendation was to use them in private methods to test program invariants. When an assert fails, a java.lang.AssertionError is thrown and is generally not intended to be caught. The idea was they could be enabled during debugging and disabled in production code (which is the default), but frankly, I don't think they ever really caught on. I haven't seen them used much.
JUnit also has asserts, in the form of many different static methods in the org.junit.Assertpackage. These are intended to verify the results of a given test. These asserts also throw a java.lang.AssertionError, but the JUnit framework is set up to catch and record those errors and to generate a report of all failures at the end of the test run. This is the more common usage of asserts, IMO.
Java 中的assert关键字是在 1.4 中添加的,旨在允许验证类中的内部一致性。最佳实践建议是在私有方法中使用它们来测试程序不变量。当断言失败时,会抛出 java.lang.AssertionError 并且通常不会被捕获。想法是它们可以在调试期间启用并在生产代码中禁用(这是默认设置),但坦率地说,我认为它们并没有真正流行起来。我还没有看到它们使用太多。
JUnit 还以org.junit.Assert包中许多不同静态方法的形式提供断言。这些旨在验证给定测试的结果。这些断言也会抛出 java.lang.AssertionError,但 JUnit 框架被设置为捕获和记录这些错误,并在测试运行结束时生成所有失败的报告。这是断言的更常见用法,IMO。
You can obviously use either one, but the JUnit asserts are far more expressive, and you don't have to worry about enabling or disabling them. On the other hand, they aren't really for use in business code.
显然您可以使用任何一种,但 JUnit 断言更具表现力,您不必担心启用或禁用它们。另一方面,它们并不是真正用于业务代码。
EDIT:Here's a code example that works:
编辑:这是一个有效的代码示例:
import org.junit.Assert;
import org.junit.Test;
public class MyTest {
@Test
public void test() {
Object alo = null;
assert alo != null // Line 9
Assert.assertNotNull(alo); // Line 10
}
}
Here's the output from a run with Java assertions disabled (the default):
这是禁用 Java 断言(默认)的运行的输出:
c:\workspace\test>java -cp bin;lib\junit-4.8.1.jar org.junit.runner.JUnitCore MyTest
JUnit version 4.8.1
.E
Time: 0.064
There was 1 failure:
1) test(MyTest)
java.lang.AssertionError:
at org.junit.Assert.fail(Assert.java:91)
...
at MyTest.test(MyTest.java:10)
...
Here's a run with Java assertions enabled (-ea):
这是启用 Java 断言 (-ea) 的运行:
c:\workspace\test>java -ea -cp bin;lib\junit-4.8.1.jar org.junit.runner.JUnitCore MyTest
JUnit version 4.8.1
.E
Time: 0.064
There was 1 failure:
1) test(MyTest)
java.lang.AssertionError:
at MyTest.test(MyTest.java:9)
...
Notice in the first example, the Java assert passes, and in the second, it fails.
请注意,在第一个示例中,Java 断言通过,而在第二个示例中,它失败。
回答by Basil Vandegriend
The assert keyword is disabled by default for the Java VM (see http://docs.oracle.com/cd/E19683-01/806-7930/6jgp65ikq/index.html). Different Java tools may or may not be configured to enable assertions by default.
Java VM 默认禁用 assert 关键字(请参阅http://docs.oracle.com/cd/E19683-01/806-7930/6jgp65ikq/index.html)。默认情况下,不同的 Java 工具可能会也可能不会被配置为启用断言。
Eclipse currently does NOT enable assertions by default when running JUnit tests. (See the discussion on https://bugs.eclipse.org/bugs/show_bug.cgi?id=45408- the reason why not is to preserve backwards compatibility).
Eclipse 当前在运行 JUnit 测试时默认不启用断言。(请参阅https://bugs.eclipse.org/bugs/show_bug.cgi?id=45408上的讨论- 原因是为了保持向后兼容性)。
However, since Eclipse 3.6 there is a simple way in Eclipse to ensure that assertions are enabled by default for JUnit tests. Open JUnit preferences (Windows | Preferences | Java | Junit) and select the option "Add '-ea' to VM arguments when creating a new JUnit launch configuration". Note that this setting won't change existing JUnit launch configurations, for which you will need to manually add the "-ea" setting to the VM argument box for each such launch configuration.
但是,从 Eclipse 3.6 开始,Eclipse 中有一种简单的方法可以确保默认情况下为 JUnit 测试启用断言。打开 JUnit 首选项(Windows | Preferences | Java | Junit)并选择选项“在创建新的 JUnit 启动配置时将‘-ea’添加到 VM 参数”。请注意,此设置不会更改现有的 JUnit 启动配置,为此您需要手动将“-ea”设置添加到每个此类启动配置的 VM 参数框中。