Question

I'm trying to test a private method and have the following setup:

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);

This works nice until I try to run the method with null as the ClassB object:

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

This results in the following exception:

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)

I'm using Powermockito 1.5. Is there any way to execute the method with null as parameter?

Was it helpful?

Solution 2

Try using argument matchers instead:

import static org.mockito.Matchers.*;

//...

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

Once you use Matchers for any of the arguments, you have to use them for all arguments. This is noted in the Mockito documentation here (see subsection Warning on argument matchers).

OTHER TIPS

Maybe try this :

Object[] params = {null, true};

then :

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

PowerMock is unable to match for the parameter types. You can explicitly tell the parameter types like this -

ClassB classBObject = null;
boolean result = Whitebox.invokeMethod(testsubject,
        "myprivatemethod", new Class<?>[] { ClassB.class,Boolean.class},classBObject,true);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top