Open In App

Mocks vs. Stubs: Choosing the Right Tool for the Job

Last Updated : 28 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The objects known as “stubs” store specified data and utilize it to generate replies for tests. In other terms, a stub is an object that looks like a genuine object but only has the methods required for testing. When we don’t want to utilize objects that might respond with actual data, we use stubs instead. The lightest and most static kind of test double is known as a stub.

Stub

 

When to Use a Stub?

  • We can implement an interface that can separate us from the third-party library if we are developing the back end of a small application that will interact with an API. That interface will eventually produce hard-coded values and serve as a stub. In conclusion, unit tests can make use of values.
  • If we are testing a single application, we will put in place a stub for the Rest APIs that it is based on. We can also make it with the aid of any internal tool created by the development team.

What is Mock?

The objects that hold method calls are mocks. The dynamic wrappers for dependencies utilized in the tests were referred to. It is used to document and validate how the Java classes interact with one another. The most potent and adaptable variation of the test doubles is known as a mock. We employ a technique called mocking (). Mocks and stubs are identical, however, you may assert against a mock object whereas you can’t with a stub. A mock object decides whether the unit test has passed or failed. There’s usually no more than one mock per test.

Mock

When to Use a Mock?

  • A Mockito framework can be used to mock the dependent classes if the back end of an application that is being developed has a lot of classes that need to be tested.
  • A mock framework like mountebank or WireMock can be used if we need to detach from the API requirements in HTTP while constructing an application’s back end. Then make a fake for the test’s dependency classes.

Stub vs Mock

Below are the differences between stub and mock: 

Parameters  Stub Mock
Data Source Stubs’ data source is hard coded. It is frequently closely connected to the test suite. The tests put up the data on the mocks.
Purpose The purpose of the stub is state verification. The purpose of mock is characteristic verification.
Created by Most stubs are handwritten, but others are produced by tools. Usually, third-party libraries like Mockito, JMock, and WireMock are used to construct mocks.
Usage Stubs are typically used for straightforward test suites. Large test suites are often where mocks are employed.
Graphics User Interface (GUI) or User Interface Stubs do not have a GUI.  Mocks have a GUI.
Advantages Free tools and a wealth of online resources are accessible here. Open-source tools and a wealth of internet resources are accessible here.
Disadvantages Due to the data being hard-coded, test cases pair closely. Used primarily by developers rather than testers.
Technical Knowledge Using stubs requires average technical knowledge. Using mocks requires significant technical knowledge.
Implemented By Stubs are implemented by developers, testers by themselves, or are produced by tools.  Developers implement mocks using third-party libraries like JMock, WireMock, etc. 

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads