问题:尝试执行 PowerMockito.doNothing() 时引发 Nullpointer 异常。
我需要创建一个 partialMockObject 类,该类将返回一个私有方法值并且对另一个私有 setter 方法不执行任何操作。
如果我用 supress() 替换 doNothing() 但我想弄清楚这一点,我就能让它工作。
代码:
@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:' org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.when(PowerMockitoStubberImpl. java:43) 在 org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.when(PowerMockitoStubberImpl.java:104)