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

前端之家收集整理的这篇文章主要介绍了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);
原文链接:https://www.f2er.com/java/126941.html

猜你在找的Java相关文章