I have a private method which take a list of integer value returns me a list of integer value. How can i use power mock to test it. I am new to powermock.Can i do the test with easy mock..? how..
From the documentation, in the section called "Common - Bypass encapsulation":
Use Whitebox.invokeMethod(..) to invoke a private method of an instance or class.
You can also find examples in the same section.
Here is a full example how to do to it:
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.powermock.reflect.Whitebox;
class TestClass {
private List<Integer> methodCall(int num) {
System.out.println("Call methodCall num: " + num);
List<Integer> result = new ArrayList<>(num);
for (int i = 0; i < num; i++) {
result.add(new Integer(i));
}
return result;
}
}
@Test
public void testPrivateMethodCall() throws Exception {
int n = 10;
List result = Whitebox.invokeMethod(new TestClass(), "methodCall", n);
Assert.assertEquals(n, result.size());
}
Whitebox.invokeMethod(myClassToBeTestedInstance, "theMethodToTest", expectedFooValue);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With