我可以延迟使用Mockito的方法响应吗? [英] Can I delay a stubbed method response with Mockito?
本文介绍了我可以延迟使用Mockito的方法响应吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我现在正在编写单元测试。我需要用Mockito模拟长期方法来测试我的实现的超时处理。 Mockito可以吗?
I'm writing unit tests now. I need to simulate long-run method with Mockito to test my implementation's timeout handling. Is it possible with Mockito?
这样的事情:
when(mockedService.doSomething(a, b)).thenReturn(c).after(5000L);
推荐答案
你可以简单地让线程进入睡眠状态时间。注意这一点 - 这样的事情可能真的会减慢你的自动测试执行速度,所以你可能想要在一个单独的套件中隔离这些测试
You could simply put the thread to sleep for the desired time. Watch out tho - such things can really slow down your automated test execution, so you might want to isolate such tests in a separate suite
它看起来类似于: / p>
It would look similar to this:
when(mock.load("a")).thenAnswer(new Answer<String>() {
@Override
public String answer(InvocationOnMock invocation){
Thread.sleep(5000);
return "ABCD1234";
}
});
这篇关于我可以延迟使用Mockito的方法响应吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文