Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass null string to a private method using jmockit while unit testing it?

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
    }
}

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);

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 -

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

like image 310
john Avatar asked Apr 15 '14 22:04

john


1 Answers

A quick glance at the documentation suggests that what you're trying to do with that signature is not possible:

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

If that's the case, then pass in the Class object as well as the instance.

Deencapsulation.invoke(tap, "parseResponse", String.class);
like image 108
Makoto Avatar answered Oct 16 '22 13:10

Makoto