Mockito - @Spy vs @Mock

后端 未结 7 1002
南旧
南旧 2020-11-29 17:33

Mockito - I understand a spy calls the real methods on an object, while a mock calls methods on the double object. Also spies are to be avoided unless there is a code smell.

7条回答
  •  孤城傲影
    2020-11-29 18:29

    Technically speaking both "mocks" and "spies" are a special kind of "test doubles".

    Mockito is unfortunately making the distinction weird.

    A mock in mockito is a normal mock in other mocking frameworks (allows you to stub invocations; that is, return specific values out of method calls).

    A spy in mockito is a partial mock in other mocking frameworks (part of the object will be mocked and part will use real method invocations).

提交回复
热议问题