【发布时间】:2020-04-23 16:42:03
【问题描述】:
我在一个实用程序类中有这行代码
System.setProperty("someProperty", <StringValue>);
当上面的代码行通过测试执行时,我不希望发生任何事情。 我已经用以下注释对类进行了注释。
@PrepareForTest({KeyStore.class, System.class})
@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"javax.management.*", "javax.security.*"})
我尝试使用以下选项什么都不做:
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:
关于在调用 System.setProperty 时我如何无能为力的任何建议。 请注意 System 是一个最终类,我们正在调用一个带有 2 个参数的静态方法。 谢谢!!
【问题讨论】:
-
不确定,但这可能会有所帮助stackoverflow.com/questions/9585323/…
标签: java unit-testing powermockito