Simulate first call fails, second call succeeds

前端 未结 5 1591
傲寒
傲寒 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:02

    The shortest way to write what you want is

    when(myMock.doTheCall()).thenReturn("Success", "you failed");
    

    When you supply mutiple arguments to thenReturn like this, each argument will be used at most once, except for the very last argument, which is used as many times as necessary. For example, in this case, if you make the call 4 times, you'll get "Success", "you failed", "you failed", "you failed".

提交回复
热议问题