AssertionFailedError异常:布尔方法无效 [英] AssertionFailedError: null on boolean method

查看:340
本文介绍了AssertionFailedError异常:布尔方法无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我测试,它有两个对象作为参数,并返回一个布尔方法。当我使用和 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)一个空白的消息参数,这已被固定在JUnit 4中(<一个href=\"http://junit.org/junit/javadoc/4.5/org/junit/Assert.html\"><$c$c>org.junit.Assert).

示例:

的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屋!

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