Java 当调用最终类的静态方法时,如何调用PowerMockito.doNothing?
我在一个实用程序类中有这行代码Java 当调用最终类的静态方法时,如何调用PowerMockito.doNothing?,java,unit-testing,powermockito,Java,Unit Testing,Powermockito,我在一个实用程序类中有这行代码 System.setProperty("someProperty", <StringValue>); 我尝试使用以下选项不执行任何操作: 1. PowerMockito.mockStatic(System.class); PowerMockito.doNothing().when(System.class, "setProperty", "someProperty", "stringValue"); 2. PowerMockito.mockS
System.setProperty("someProperty", <StringValue>);
我尝试使用以下选项不执行任何操作:
1. PowerMockito.mockStatic(System.class);
PowerMockito.doNothing().when(System.class, "setProperty", "someProperty", "stringValue");
2. PowerMockito.mockStatic(System.class);
PowerMockito.doNothing().when(System.class, "setProperty", Mockito.any(String.class), Mockito.any(String.class));
3. PowerMockito.mockStatic(System.class);
PowerMockito.doAnswer(new org.mockito.stubbing.Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return null; //does nothing
}
}).when(System.class, "setProperty", "someProperty", "stringValue");
关于如何在调用System.setProperty时不执行任何操作的任何建议。
请注意,System是最后一个类,我们调用的是一个带有2个参数的静态方法。
谢谢 只有void方法不能执行任何操作()
setProperty()返回字符串
使用
不确定,但这可能会有帮助
1. PowerMockito.mockStatic(System.class);
PowerMockito.doNothing().when(System.class, "setProperty", "someProperty", "stringValue");
2. PowerMockito.mockStatic(System.class);
PowerMockito.doNothing().when(System.class, "setProperty", Mockito.any(String.class), Mockito.any(String.class));
3. PowerMockito.mockStatic(System.class);
PowerMockito.doAnswer(new org.mockito.stubbing.Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return null; //does nothing
}
}).when(System.class, "setProperty", "someProperty", "stringValue");
org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
PowerMockito.mockStatic(System.class);
PowerMockito.when(System.setProperty(anyString(), anyString())).thenReturn("test");