AssertionFailedError异常:布尔方法无效 [英] AssertionFailedError: null on boolean method
问题描述
我测试,它有两个对象作为参数,并返回一个布尔方法
。当我使用和 assertTrue
或 assertFalse
对有问题,我得到了以下测试失败的方法: junit.framework.AssertionFailedError:空
我知道,我传递的参数无效,并很可能会引起的的方法中NPE 但那不是发生了什么,而不是测试失败。
请注意:我使用布尔
,而不是的布尔。
样code:
类:
公共类MyClass的{
公共布尔美孚(MyObject1 LHS,RHS MyObject2){
//做一点事
//返回的东西
}
}
测试:
.... // MyClass的的动初始化等。
@测试
公共无效命名为testFoo(){
assertTrue(myClass.foo(新MyObject1(),新MyObject2());
}
空
显示了作为一个JUnit 3断言消息(<一个href=\"http://junit.org/junit/javadoc/4.5/junit/framework/Assert.html\"><$c$c>junit.framework.Assert$c$c>)一个空白的消息参数,这已被固定在JUnit 4中(<一个href=\"http://junit.org/junit/javadoc/4.5/org/junit/Assert.html\"><$c$c>org.junit.Assert$c$c>).
示例:
的的JUnit 3:的
assertTrue(假)
有相同的堆栈跟踪 assertTrue(空,FALSE)
的的JUnit 4:的
assertTrue(假)
有相同的堆栈跟踪 assertTrue(,FALSE)
I am testing a method that takes two objects as parameters and returns a boolean
. When I use and assertTrue
or assertFalse
on the method in question I get the following test failure: junit.framework.AssertionFailedError: null
.
I know that I am passing invalid parameters and will likely be causing a NPE within the method but that is not what is happening, instead the test is failing.
Note: I am using boolean
and not 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());
}
"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
).
Example:
JUnit 3:
assertTrue(false)
has the same stack trace as assertTrue("null", false)
JUnit 4:
assertTrue(false)
has the same stack trace as assertTrue("", false)
这篇关于AssertionFailedError异常:布尔方法无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!