When we writing a JUnit test case, we sometimes need to get private member variable to verify the testing result or invoke private function to simulate software process.
We can use the object named PrivateAccessor to get private member like below.
To get private variable:
Integer.parseInt(PrivateAccessor.getPrivateField(Object, "Index").toString());
To invoke private function:
// The arguments to be passed to method
Object[] params = new Object[1];
params[0] = 0;
PrivateAccessor.invokePrivateMethod(Object, "functionName", params);
Reference:
http://junit-addons.sourceforge.net/junitx/util/PrivateAccessor.html
沒有留言:
張貼留言