带有null参数的Powermock Whitebox调用

时间:2013-06-11 09:35:00

标签: null mockito powermock white-box

我正在尝试测试私有方法并进行以下设置:

public class MyClass {
  private boolean myprivatemethod(ClassB classBObject, boolean b) {
    // do stuff here
    someOtherMethod();
  }

  private void someOtherMethod() {
    // more stuff
  }
}

final MyClass testsubject = PowerMockito.spy(new MyClass());
// spy is needed because "someOtherMethod" is mocked 
// which is not shown here for simplicity
ClassB classBObject = mock(ClassB.class);

boolean result = Whitebox.invokeMethod(testsubject, "myprivatemethod", classBObject, true);

这很好用,直到我尝试使用null作为ClassB对象运行方法:

boolean result = Whitebox.invokeMethod(testsubject, "myprivatemethod", null, true);

这会导致以下异常:

java.lang.NullPointerException
    at java.lang.Class.isAssignableFrom(Native Method)
    at org.powermock.reflect.internal.WhiteboxImpl.checkIfParameterTypesAreSame(WhiteboxImpl.java:2432)
    at org.powermock.reflect.internal.WhiteboxImpl.getMethods(WhiteboxImpl.java:1934)
    at org.powermock.reflect.internal.WhiteboxImpl.getBestMethodCandidate(WhiteboxImpl.java:1025)
    at org.powermock.reflect.internal.WhiteboxImpl.findMethodOrThrowException(WhiteboxImpl.java:948)
    at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:882)
    at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:713)
    at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:401)
    at test.myproject.TestMyClass.testMyClass(TestMyClass.java:10)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at junit.framework.TestCase.runTest(TestCase.java:154)
    at junit.framework.TestCase.runBare(TestCase.java:127)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:118)
    at junit.framework.TestSuite.runTest(TestSuite.java:208)
    at junit.framework.TestSuite.run(TestSuite.java:203)
    at org.powermock.modules.junit3.internal.impl.PowerMockJUnit3RunnerDelegateImpl.run(PowerMockJUnit3RunnerDelegateImpl.java:113)
    at org.powermock.modules.junit3.internal.impl.JUnit3TestSuiteChunkerImpl.run(JUnit3TestSuiteChunkerImpl.java:165)
    at org.powermock.modules.junit3.PowerMockSuite.run(PowerMockSuite.java:51)
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我正在使用Powermockito 1.5。 有没有办法以null作为参数执行方法?

3 个答案:

答案 0 :(得分:1)

尝试使用参数匹配器:

import static org.mockito.Matchers.*;

//...

boolean result = Whitebox.invokeMethod(testsubject,
    "myprivatemethod", isNull(ClassB.class), eq(true));

一旦将Matchers用于任何参数,就必须将它们用于所有参数。这在Mockito文档here中有所说明(参见关于参数匹配器的警告小节)。

答案 1 :(得分:1)

也许试试这个:

Object[] params = {null, true};

然后:

Whitebox.invokeMethod(testsubject, "myprivatemethod", params);

答案 2 :(得分:0)

PowerMock无法匹配参数类型。您可以明确告诉参数类型 -

ClassB classBObject = null;
boolean result = Whitebox.invokeMethod(testsubject,
        "myprivatemethod", new Class<?>[] { ClassB.class,Boolean.class},classBObject,true);