问题:试图执行PowerMockito.doNothing()时提出的Nullpointer例外。
我需要建立一个部分MockObject的班级,将回报一种私人方法价值,对另一种私人制造方法则无所作为。
如果我用“压力”取代“Nothing()”的话,我就能够这样做,但我要指出这一点。
Code:
@Test
public void testPowerMockito() throws Exception
{
final String methodName1 = "Method1";
final String methodName2 = "Method2";
//Using PowerMockito
ObjectToTest partialMockObject = PowerMockito.spy(new ObjectToTest());
//Mock the private method, expect that a false
PowerMockito.doReturn(false).when( partialMockObject,methodName1 );
//Do Nothing on the void private setter
PowerMockito.doNothing().when( ObjectToTest.class,methodName2 );
String result = partialMockObject.methodToTest();
assertEquals("Fail","",result);
//Confirms that the private method was called
PowerMockito.verifyPrivate(partialMockObject).invoke(methodName1);
PowerMockito.verifyPrivate(partialMockObject).invoke(methodName1);
}
StackTrace: java.lang.NullPointerException at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.addAnswersForStubbing(PowerMockitoStubberImpl.java:68) at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.when(PowerMockitoStubberImpl.java:43) at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.when(PowerMockitoStubberImpl.java:104)