我为返回字符串的私有函数编写了一个 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);
}
我怎么能跑?