java AssertionFailedError:布尔方法为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17112757/
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-11-01 01:02:25  来源:igfitidea点击:

AssertionFailedError: null on boolean method

javajunitbooleanassert

提问by AnthonyW

I am testing a method that takes two objects as parameters and returns a boolean. When I use and assertTrueor assertFalseon the method in question I get the following test failure: junit.framework.AssertionFailedError: null.

我正在测试一种将两个对象作为参数并返回一个boolean. 当我使用和assertTrueassertFalse在有问题的方法上时,我得到以下测试失败:junit.framework.AssertionFailedError: null.

I know that I am passing invalid parameters and will likely be causing a NPEwithin the method but that is not what is happening, instead the test is failing.

我知道我传递了无效参数,并且可能会导致方法中出现NPE,但这不是正在发生的事情,而是测试失败了。

Note: I am using booleanand not Boolean.

注意:我正在使用boolean而不是Boolean

Sample Code:

示例代码:

Class:

班级:

public class MyClass{
  public boolean foo(MyObject1 lhs, MyObject2 rhs){
    //doSomething
    //return something
  }
}

Test:

测试:

.... //initilization of myClass, etc.
@Test
public void testFoo(){
  assertTrue(myClass.foo(new MyObject1(), new MyObject2());
}

回答by AnthonyW

"null" shows up as the message in a JUnit 3 assert (junit.framework.Assert) with a blank message parameter. This has been fixed in JUnit 4 (org.junit.Assert).

" null" 显示为junit.framework.Assert带有空白消息参数的 JUnit 3 assert ( ) 消息。这已在 JUnit 4 ( org.junit.Assert) 中修复。

Example:

例子:

JUnit 3:

JUnit 3:

assertTrue(false)has the same stack trace as assertTrue("null", false)

assertTrue(false)具有相同的堆栈跟踪 assertTrue("null", false)

JUnit 4:

JUnit 4:

assertTrue(false)has the same stack trace as assertTrue("", false)

assertTrue(false)具有相同的堆栈跟踪 assertTrue("", false)