我正在尝试测试私有方法并具有以下设置:
公共类 MyClass { private boolean myprivatemethod(ClassB classBObject, boolean b) { // 在这里做事 一些其他方法(); } 私人无效 someOtherMethod() { // 更多东西 } } final MyClass testsubject = PowerMockito.spy(new MyClass()); // spy 是必需的,因为 "someOtherMethod" 被模拟了 // 为简单起见,此处未显示 ClassB classBObject = mock(ClassB.class); 布尔结果 = Whitebox.invokeMethod(testsubject, "myprivatemethod", classBObject, true);
null
在我尝试将方法作为 ClassB 对象运行之前,这很好用:
布尔结果 = Whitebox.invokeMethod(testsubject, "myprivatemethod", null, true);
这会导致以下异常:
java.lang.NullPointerException 在 java.lang.Class.isAssignableFrom(Native Method) 在 org.powermock.reflect.internal.WhiteboxImpl.checkIfParameterTypesAreSame(WhiteboxImpl.java:2432) 在 org.powermock.reflect.internal.WhiteboxImpl.getMethods(WhiteboxImpl.java:1934) 在 org.powermock.reflect.internal.WhiteboxImpl.getBestMethodCandidate(WhiteboxImpl.java:1025) 在 org.powermock.reflect.internal.WhiteboxImpl.findMethodOrThrowException(WhiteboxImpl.java:948) 在 org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:882) 在 org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:713) 在 org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:401) 在 test.myproject.TestMyClass.testMyClass(TestMyClass.java:10) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在 java.lang.reflect.Method.invoke(Method.java:597) 在 junit.framework.TestCase.runTest(TestCase.java:154) 在 junit.framework.TestCase.runBare(TestCase.java:127) 在 junit.framework.TestResult$1.protect(TestResult.java:106) 在 junit.framework.TestResult.runProtected(TestResult.java:124) 在 junit.framework.TestResult.run(TestResult.java:109) 在 junit.framework.TestCase.run(TestCase.java:118) 在 junit.framework.TestSuite.runTest(TestSuite.java:208) 在 junit.framework.TestSuite.run(TestSuite.java:203) 在 org.powermock.modules.junit3.internal.impl.PowerMockJUnit3RunnerDelegateImpl.run(PowerMockJUnit3RunnerDelegateImpl.java:113) 在 org.powermock.modules.junit3.internal.impl.JUnit3TestSuiteChunkerImpl.run(JUnit3TestSuiteChunkerImpl.java:165) 在 org.powermock.modules.junit3.PowerMockSuite.run(PowerMockSuite.java:51) 在 org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130) 在 org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
我正在使用 Powermockito 1.5。有没有办法用null
as 参数执行方法?