Can I delay a stubbed method response with Mockito?

后端 未结 4 1541
执笔经年
执笔经年 2020-12-07 17:21

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?

Something li

4条回答
  •  粉色の甜心
    2020-12-07 18:10

    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

    It would look similar to this:

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

提交回复
热议问题