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.
I like the simplicity of this recommendation:
- If you want to be safe and avoid calling external services and just want to test the logic inside of the unit, then use mock.
- If you want to call external service and perform calling of real dependencies, or simply say, you want to run the program as it is and just stub specific methods, then use spy.
Source: https://javapointers.com/tutorial/difference-between-spy-and-mock-in-mockito/
A common difference is:
- If you want to directly stub the method(s) of a dependency, then Mock that dependency.
- If you want to stub the data in a dependency so that all its methods return testing values that you need, then Spy that dependency.