1.

When and why should we use spy?

Answer»

Spy is a partial mock OBJECT supported by the Mockito framework. FOLLOWING are the features of spy objects:

  • Whenever mock is not set up, interaction on spy results in REAL METHOD calls. Spy objects allow verifying interactions such as whether the method was called and how many times it was called, etc.
  • They provide flexibility for setting up partial mocks. For instance, if an object has 2 methods, and we want one method to be mocked and the other to be called actually, then we can use spies.

The main DIFFERENCE between mock and spy is that in mock, we create a complete fake object whereas, in spy, we have a hybrid of a real object and fake/stubbed methods.



Discussion

No Comment Found