微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.jb51.cc/java/126941.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐