AssertionFailedError:布尔方法为 null

2024-02-21

我正在测试一个方法,该方法接受两个对象作为参数并返回一个boolean。当我使用和assertTrue or assertFalse对于有问题的方法,我得到以下测试失败:junit.framework.AssertionFailedError: null.

我知道我传递了无效参数,并且可能会导致NPE http://docs.oracle.com/javase/6/docs/api/java/lang/NullPointerException.html在该方法内,但这不是正在发生的事情,而是测试失败了。

注意:我正在使用boolean并不是Boolean http://docs.oracle.com/javase/6/docs/api/java/lang/Boolean.html.

示例代码:

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());
}

"null" 显示为 JUnit 3 断言中的消息 (junit.framework.Assert http://junit.org/junit/javadoc/4.5/junit/framework/Assert.html) 带有空白消息参数。这已在 JUnit 4 中修复(org.junit.Assert http://junit.org/junit/javadoc/4.5/org/junit/Assert.html).

Example:

JUnit 3:

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

JUnit 4:

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

AssertionFailedError:布尔方法为 null 的相关文章

随机推荐