单元测试:断言不起作用? [英] Unit test: assert not work?

查看:585
本文介绍了单元测试:断言不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在申请UnitTest一段时间,今天我遇到了一些非常奇怪的事情。考虑以下代码:

I'm applying UnitTest just for a while, and today I met something very strange. Considering the following code:

TestObject alo = null;
assert alo != null; // Pass!!!
Assert.assertNotNull(alo); // Fail, as expected.

我google了一下,发现assert是java内置的,而assertNotNull是JUnit支持的。但我无法理解为什么断言不抱怨null对象?

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?

推荐答案

Hoang,我想你是在Java语言断言和JUnit断言之间有点混淆。

Hoang, I think you're getting a little confused between Java language asserts and JUnit asserts.


  • Java中的断言关键字是在1.4中添加,旨在允许验证类中的内部一致性。最佳实践建议是在私有方法中使用它们来测试程序不变量。当断言失败时,抛出java.lang.AssertionError并且通常不会被捕获。这个想法是他们可以在调试期间启用它们并在生产代码中禁用(这是默认设置),但坦率地说,我认为它们并没有真正流行起来。我没有看到它们用得太多。

  • The assert keyword 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也有 org.junit.Assert中许多不同静态方法的形式断言包。这些旨在验证给定测试的结果。这些断言也会抛出一个java.lang.AssertionError,但是JUnit框架被设置为捕获并记录这些错误,并在测试运行结束时生成所有失败的报告。这是断言的更常见用法,IMO。

JUnit also has asserts, in the form of many different static methods in the org.junit.Assert package. 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.

你显然可以使用其中任何一个,但JUnit断言是更具表现力,您不必担心启用或禁用它们。另一方面,它们并不真正用于商业代码。

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.

编辑:这是一个有效的代码示例:

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
  }
}

以下是运行的输出禁用Java断言(默认值):

Here's the output from a run with Java assertions disabled (the default):

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)
  ...

这是启用Java断言的运行( -ea):

Here's a run with Java assertions enabled (-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)
  ...

注意在第一个例子中,Java断言传递,并在seco中它失败了。

Notice in the first example, the Java assert passes, and in the second, it fails.

这篇关于单元测试:断言不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆