【发布时间】:2025-12-05 22:20:03
【问题描述】:
下面是我在DataTap 类中的私有方法,我正在尝试使用 jmockit 对这个私有方法进行 junit 测试 -
private void parseResponse(String response) throws Exception {
if (response != null) {
// some code
}
}
所以下面是我写的 junit 测试,但对于 null 情况,它以某种方式在 junit 测试本身上抛出 NPE。
DataTap tap = new DataTap();
Deencapsulation.invoke(tap, "parseResponse", "hello");
// this line throws NPE
Deencapsulation.invoke(tap, "parseResponse", null);
所以我的问题是 - 有什么方法可以使用 JMOCKIT 作为 junit 测试的一部分将 null string 传递给 parseResponse 方法吗?
这就是我在那条线上看到的 -
null 类型的参数应该显式转换为 Object[] 可变参数方法的调用 invoke(Object, String, Object...) 从类型解封装。它也可以转换为 Object 对于可变参数调用
【问题讨论】:
-
该代码应该可以工作。我认为问题出在其他地方。
-
这是我在
The argument of type null should explicitly be cast to Object[] for the invocation of the varargs method invoke(Object, String, Object...) from type Deencapsulation. It could alternatively be cast to Object for a varargs invocation那行看到的,它确实失败了。