Javatpoint Logo
Javatpoint Logo

Mock vs. Stub vs. Spy


Mocks are the objects that store method calls. It referred to as the dynamic wrappers for dependencies used in the tests. It is used to record and verify the interaction between the Java classes. A mock is known as the most powerful and flexible version of the test doubles. We use a method for mocking is called mock().

The main function of using mocks is that it gives full control over the behavior of the mocked objects. The mock objects are generally used for behavior verification. The term behavior means to check the correct methods and paths that are applied to the objects.

Mocks are mostly created by using a library or a mocking framework like Mockito, JMock, and EasyMock. It is used for testing a large suite of tests where stubs are not sufficient. One of the essential functions of mock is, we can verify how many times a given method is called.

The following code snippet shows how to use mock().


Stubs are the objects that hold predefined data and uses it to give responses during tests. In other words, a stub is an object that resembles a real object with the minimum number of methods needed for a test. Stubs are used when we don't want to use objects that would give a response with real data. A stub is referred to as the lightest, and the most static version of the test doubles.

The main functions of the stubs are:

  • It always returns the predefined output regardless of the input.
  • It can be used to resemble the database objects.
  • Stubs are used to reduce the complexities that occur during the creation of the real objects. They are mainly used for performing state verification.


Spies are known as partially mock objects. It means spy creates a partial object or a half dummy of the real object by stubbing or spying the real ones. In spying, the real object remains unchanged, and we just spy some specific methods of it. In other words, we take the existing (real) object and replace or spy only some of its methods.

Spies are useful when we have a huge class full of methods, and we want to mock certain methods. In this scenario, we should prefer using spies rather than mocks and stubs. It calls the real method behavior, if the methods are not stubbed.

In Mockito, spy() method is used for creating spy objects. It allows us to call the normal methods of the real object. The following code snippet shows how to use the spy() method.

Difference between Stub and Mock

Parameters Stub Mock
Data Source The data source of stubs is hardcoded. It is usually tightly coupled to the test suite. Data on mocks is set up by the tests.
Created by Stubs are usually handwritten, and some are generated by tools. Mocks are usually created by using the third-party library such as Mockito, JMock, and WireMock.
Usage Stubs are mainly used for simple test suites. Mocks are mainly used for large test suites.
Graphics User Interface (GUI) Stubs do not have a GUI. Mocks have a GUI.

Following are some differences between the mock and spy:

Parameters Mock Spy
Usage Mocks are used to create fully mock or dummy objects. It is mainly used in large test suites. Spies are used for creating partial or half mock objects. Like mock, spies are also used in large test suites.
Default behavior When using mock objects, the default behavior of methods (when not stubbed) is do nothing (performs nothing.) When using spy objects, the default behavior of the methods (when not stubbed) is the real method behavior.

In our previous tutorials, we have discussed some examples of stubbing, mocking, and spying. For better understanding the difference between stubbing, mocking, and spying, go through the examples.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA