加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 00:01:57 所属栏目:Java 来源:网络整理
导读:下面是我的DataTap类中的私有方法,我试图使用jmockit对这个私有方法进行junit测试 – private void parseResponse(String response) throws Exception { if (response != null) { // some code }} 所以下面是我写的junit测试但是对于null情况,它会在junit测
下面是我的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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读