java – 如何在单元测试时使用jmockit将空字符串传递给私有方法?

下面是我的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,null);

所以我的问题是 – 有什么办法可以使用JMOCKIT作为junit测试的一部分将空字符串传递给parseResponse方法

这就是我在这条线上看到的 –

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

解决方法

快速浏览 at the documentation表明您无法使用该签名进行操作:

…if a null value needs to be passed,the Class object for the parameter type must be passed instead

如果是这种情况,则传入Class对象以及实例.

Deencapsulation.invoke(tap,String.class);

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...