问题描述
下面是我的DataTap
类中的私有方法,我正在尝试使用jmockit对这个私有方法进行junit测试-
Below is my private method in my DataTap
class and I am trying to junit test this private method using jmockit -
private void parseResponse(String response) throws Exception {
if (response != null) {
// some code
}
}
所以下面是我编写的junit测试,但是对于空值情况,它会以某种方式在junit测试本身上抛出NPE.
So below is the junit test I wrote but for null case, it is throwing NPE somehow on the junit test itself.
DataTap tap = new DataTap();
Deencapsulation.invoke(tap, "parseResponse", "hello");
// this line throws NPE
Deencapsulation.invoke(tap, "parseResponse", null);
所以我的问题是-有什么方法可以使用JMOCKIT将null string
传递给parseResponse
方法作为junit测试的一部分?
So my question is - Is there any way I can pass null string
to parseResponse
method using JMOCKIT as part of junit testing?
这就是我在那条线上看到的-
This is what I am seeing on that line -
推荐答案
快速浏览文档中的提示您无法尝试使用该签名:
A quick glance at the documentation suggests that what you're trying to do with that signature is not possible:
在这种情况下,请同时传入Class
对象和实例.
If that's the case, then pass in the Class
object as well as the instance.
Deencapsulation.invoke(tap, "parseResponse", String.class);
这篇关于如何在单元测试时使用jmockit将空字符串传递给私有方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!