Mockito/PowerMockito在java中测试System.currentTimeMillis()
我在班上有下面的方法Mockito/PowerMockito在java中测试System.currentTimeMillis(),java,mockito,powermock,Java,Mockito,Powermock,我在班上有下面的方法 public boolean isExecutionDone() { boolean flag = false; long nowPlus5Minutes = System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(5); while (!flag) { if (System.currentTimeMillis() == nowPlus5Minutes) {
public boolean isExecutionDone() {
boolean flag = false;
long nowPlus5Minutes = System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(5);
while (!flag) {
if (System.currentTimeMillis() == nowPlus5Minutes) {
flag = true;
}
}
return flag;
}
我正试图编写一个junit测试用例
@Test
public void isExecutionDoneTest(){
PowerMockito.mockStatic(System.class);
// PowerMockito.when(System.currentTimeMillis()+TimeUnit.MINUTES.toMillis(5)).thenReturn(1603768243291l);
// PowerMockito.when(System.currentTimeMillis()).thenReturn(1603768543291l);
PowerMockito.when(System.currentTimeMillis()).thenReturn(1l);
PowerMockito.when(System.currentTimeMillis()).thenReturn(300005l);
Mockito.when(myTestClass.isExecutionDone()).thenReturn(true);
}
我的MyTestClass用@InjectMock注释,TestClass用下面的注释注释
@PowerMockIgnore("javax.management.*")
@RunWith(PowerMockRunner.class)
@PrepareForTest(MyTestClass .class)
我如何将junit测试用例修改为方法ISExecutionOne()。好吧,我想你的模拟没有达到你想要的效果,尽管你没有真正解释观察到的行为。请注意,在这个用例中存在这样一种情况,即允许注入具有不同行为的时钟,主要用于测试。不过,这需要修改您当前的代码,不确定这是否适合您。