Simulate first call fails, second call succeeds

前端 未结 5 1589
傲寒
傲寒 2021-01-30 15:24

I want to use Mockito to test the (simplified) code below. I don\'t know how to tell Mockito to fail the first time, then succeed the second time.

for(int i = 1;         


        
5条回答
  •  借酒劲吻你
    2021-01-30 16:13

    I have a different situation, I wanted to mock a void function for the first call and run it normally at the second call.

    This works for me:

    Mockito.doThrow(new RuntimeException("random runtime exception"))
           .doCallRealMethod()
           .when(spy).someMethod(Mockito.any());
    

提交回复
热议问题