【发布时间】:2012-03-27 10:30:51
【问题描述】:
我为返回字符串的私有函数编写了 JUnit 测试。它工作正常。
public void test2() throws Exception
{
MyHandler handler = new MyHandler();
Method privateStringMethod = MyHandler.class.getDeclaredMethod("getName", String.class);
privateStringMethod.setAccessible(true);
String s = (String) privateStringMethod.invoke(handler, 852l);
assertNotNull(s);
}
我还有一个返回布尔值的函数,但这不起作用。
但是我得到一个编译时错误说Cannot cast from Object to boolean.
public void test1() throws Exception
{
MyHandler handler = new MyHandler();
Method privateStringMethod = MyHandler.class.getDeclaredMethod("isvalid", Long.class);
privateStringMethod.setAccessible(true);
boolean s = (boolean) privateStringMethod.invoke(handler, 852l);
assertNotNull(s);
}
我怎么跑?
【问题讨论】:
-
isvalid() 是返回 boolean 还是 Boolean?
-
@Jim 它返回布尔值。