使用Mockito时mock()和stub()有什么区别?

时间:2011-03-10 14:36:29

标签: java mockito

他们似乎都做同样的事情 - 为什么你会优先使用另一个?

org.mockito.Mockito.stub()
org.mockito.Mockito.mock()

1 个答案:

答案 0 :(得分:16)

您可以使用模拟对象来验证是否已按预期方式调用它。在Mockito中,模拟对象是自动存根,并且显式验证。

来自Mockito的"Why do we need another mocking framework?"

 Separation of stubbing and verification. Should let me code in line with intuition: 
 stub before execution, selectively verify interactions afterwards. I don’t 
 want any verification-related code before execution.

您可以在调用之前存根调用行为。例如(来自Mockito主页):

 when( mockedList.get(0)).thenReturn( "first" );

您可以在调用后验证与模拟对象的交互。例如:

 verify( mockedList ).add("one");
相关问题