Calling Mockito.when multiple times on same object?

前端 未结 2 1521
长情又很酷
长情又很酷 2020-12-29 10:09

When trying to use Mockito with Spring, by creating the Mock object via a bean declaration...



        
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-29 10:37

    The simple answer is:
    when you write Mockito.when(object.fooMethod()).then() then fooMethod() is actually called.
    Another point is that we can't observe it first time, because it called on mocked object. But when we write when for the second time then we have some behavior for fooMethod() (we set it previously, in your case it Exception).

    To check this better you can spy object:

    Bar spyBar = Mockito.spy(Bar.class)
    when(spyBar.fooMethod()).then()...
    

    and fooMethod() will be actually called.

提交回复
热议问题