我可以使用 Mockito 延迟存根方法响应吗? [英] Can I delay a stubbed method response with Mockito?

查看:26
本文介绍了我可以使用 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

它看起来像这样:

when(mock.load("a")).thenAnswer(new Answer<String>() {
   @Override
   public String answer(InvocationOnMock invocation){
     Thread.sleep(5000);
     return "ABCD1234";
   }
});

这篇关于我可以使用 Mockito 延迟存根方法响应吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆