我需要访问没有参数的单元测试中的私有方法。getMethod()的两个实现似乎需要参数类型作为最后一个参数。
有别的办法绕过这件事吗?
我试过:
WhiteBox.getMethod(myClass,"method",null); 发布于 2014-07-28 12:34:21
这次考试通过了powermock-reflect-1.5.5考试。
类:
public class Util {
private void method() {}
private static void staticMethod() {}
}测试:
import static org.junit.Assert.assertNotNull;
import java.lang.reflect.Method;
import org.junit.Test;
import org.powermock.reflect.Whitebox;
public class UtilTest {
@Test
public void testMethod() {
Method method = Whitebox.getMethod(Util.class, "method");
assertNotNull(method);
}
@Test
public void testStaticMethod() {
Method method = Whitebox.getMethod(Util.class, "staticMethod");
assertNotNull(method);
}
}发布于 2014-07-28 12:18:56
在java.lang.Class中,方法getMethod是
Method getMethod(String name, Class<?>... parameterTypes)这意味着你可以打电话给
clazz.getMethod( "method" );作为一种使用参数的方法。所以您的Whitebox方法可以被调用
Whitebox.getMethod(myClass,"method");发布于 2014-07-28 12:40:09
WhiteBox.getMethod(MyClass.class,"method",null);https://stackoverflow.com/questions/24995033
复制相似问题