How to match "any" parameter type while mocking private method in Jmockit

I have a problem using jmockit for the following scenario. Have done some research on the internet but haven't been able to find the answers yet.

In the recording phase, I set a wait on the object that is partially mocked. In doing this, I would like to mock a private method with a single parameter. But I'm not very interested in this parameter value. I want to match all calls to this private method against whatever argument passed to it. How to do it in Jmockit. Is there a way?

new Expectations(student) {
    {
        Deencapsulation.invoke(student, "setDepartment", (Department) any);
        result = new Delegate<Student>() {
            public void setDepartment(Department dept) {
                System.out.println("Mocked setDepartment() methodd.....");
            }
        };
    }
};  

      

In the above code (Department) any

can not be transmitted, since the method Deencapsulation.invoke(...)

does not take a value null

.

+3


source to share


1 answer


Pay attention to the API documentation for the field any

:

"In calls to non-accessible methods or constructors (for example, using Deencapsulation.invoke (Object, String, Object ...)), use withAny (T) instead.



That is, you need to use withAny(Department.class)

with a call invoke(...)

.

+5


source







All Articles