Related Articles

Stub vs. Mock - Top Differences You Must Know

stub vs. mock

Mocks and Stubs have long been associated with developing and running tests in software development when stubs and mocks unit testing is performed. Both mocks and stubs are test doubles, which are stand-ins for when developers don't want to use the real thing during tests.

If you are a developer, you may have been confused about stub vs. mock. In this blog, let me introduce you to stub and mock and stub testing details. I will also get into the differences between them based on different parameters. Let’s get started!

What exactly is a stub?

A stub interface simulates an actual object by using a few methods. It's an object with pre-existing data that delivers a constant value regardless of input. You can utilize it when the test suite is simple and hard-coded values aren't an issue. Both developers and testers also use it. However, you cannot share it due to compatibility issues caused by hard-coding of resources, deployment dependencies, and platforms.

You can also use the stub for Hypertext Transfer Protocol (HTTP) communication, which some refer to as a virtual service. Furthermore, it can be used to match database objects. In other words, a stub can be used as a dummy module to test an application while the actual module is being developed. Furthermore, the stub alleviates any issues that may arise during the implementation of the actual object.

It's essential to understand mock before we get into stub vs. mock.

What is the definition of mock?

Mock is an interface that we program to compare the outputs from the tests to the expected outcomes. You can frequently use third-party libraries such as Mockito and JMock to accomplish this. It also comes in handy when you have an extensive test suite, and each test demands a different set of data.

Maintaining a stub becomes too expensive in this situation. A mock allows the test to keep its data configuration. Both developers and testers can also use it. However, due to compatibility issues arising from hard-coding of resources, deployment dependencies, and platforms, you cannot share it with a larger community.

A mock can also track how many times we call a method, function, or object and the order of calls. It also monitors communication between classes. For mocking reasons, we can utilize the method ‘mock()’.

Stub vs. mock; the key differences you must know

Many people mistook fake objects with the usual testing concept of using stubs when they were originally introduced. Since then, it appears that individuals have gained a greater understanding of the disparities. However, understanding mocks and other types of test doubles is necessary to completely comprehend how people utilize mocks. If this is a new term to you, don't panic; wait a few paragraphs and everything will become clear.

When you undertake this kind of testing, like using stub for testing, you concentrate on one aspect of the software at a time, hence the phrase "unit testing". The challenge is that making a single unit work typically necessitates the usage of multiple units, hence the requirement for a warehouse in our scenario.

The terminology for discussing this quickly becomes jumbled, with terms like stub, mock, fake, and dummy being employed. The vocabulary from Gerard Meszaros' book is used for this article. It's not the most popular vocabulary, but it's a decent one.

Meszaros uses the term Test Double to refer to any type of fictitious object that is utilized in place of a genuine object for testing reasons. The name stems from the idea of a movie stunt double. (He wanted to avoid using any names that were already well-known.)

Meszaros explained five different types of doubles:

  • It is customary to pass around dummy objects that are never utilized. Typically, they're just used to populate parameter lists.
  • Although mock objects have functional implementations, they frequently take shortcuts that make them unsuitable for production.
  • Stubs reply to calls during the test with prepared responses, frequently not responding to anything that isn't set in for the test.
  • Spies are stubs with some information recorded about them based on how they were called. An email service that keeps track of how many messages it sends is one example.
  • What we're talking about here are mocks, which are objects pre-programmed with expectations that create a specification of the calls they should get.
  • Only mocks insist on behavior verification among these types of doubles. State verification can and is used by the other doubles.
  • Mocks perform similarly to other doubles during the exercise phase, as they must convince the SUT that it is conversing with genuine collaborators; however, mocks differ in the setup and verification phases.

We'll need to expand our example to learn more about test doubles. Many individuals only utilize a test double if working with the genuine object is difficult. If we mentioned we wanted to send an email message if we couldn't fill an order, that would be a more usual case for a test duplicate. The issue is that while testing, you don't want to send actual email messages to clients. Instead, you should build a test version of the email system that you can alter and manage.

Let’s summarize mock vs. stub below.


Stub vs. Mock: State testing vs. Behavioral testing


According to the principle of Test, there can be several stubs in one test, but in the case of mock, there is only one test.


Test lifecycle with stubs: stubs and mocks unit testing

  • Setup - Prepare the object to be tested and its collaborators.
  • Exercise - Test the functionality.
  • Verify state - Using asserts to test the object's state.
  • Teardown - Clean up resources.

Test lifecycle with mocks: mock vs. stub, stub testing

Setup data - Prepare objects to be tested. Exercise - Test the functionality. Verify expectations - Verify if correct methods have been followed in mock. Verify state - Using asserts to test the object's state. Teardown - Clean up resources.


  • Developers execute it themselves or using tools.
  • Developers execute it using third-party libraries like JMock, Mokito and WireMock.


  • Stubs: Mostly used for simple test suites.
  • Mocks: Mostly used for large test suites.


Stubs and Mock objects are common ways of testing a unit. The processes help to reduce complexity. While a Stub simulates real objects with the minimum methods needed for a test, Mock objects are used to check if the correct techniques and paths are applied to the objects. Additionally, Mock is very useful when we have an extensive test suite, and each test requires a unique data set. This article aims to help you understand the fundamental differences between mocking and stubbing in java unit testing. You can use these testing techniques to test your module if you are a Java developer.

Frequently Asked Questions

No, mocks and stubs are not same. A typical stub is a database connection that enables you to simulate any scenario even in the absence of an actual database. A mock is a made-up class that can be investigated for its interactions with the actual class being tested after the test has been completed.

You can restrict or limit the behaviour of the collaborators by using test doubles. This separates the thing from outside influences. Additionally, employing test doubles allows you to eliminate expensive dependencies that can be out of your control, cause indeterminacy or performance penalties, or all of the above.

A stub is a little piece of code used in testing to replace another component. Using a stub has the advantage of returning consistent results, which makes the test easier to construct. When a unit being tested has external dependencies, unit testing procedures such as mocking are applied. Mocking serves to separate and concentrate attention on the code being tested from the behaviour or state of external dependencies.

Test double is a generic term used for mock, stub and fake. Mock test double are essentially practise exams that are created solely using the most recent exam format and course material. These are practise examinations that mimic real ones so that candidates can assess their true potential.

A stub is a little software procedure that stands in for a lengthier one that might be loaded later or that is run from a different location. For instance, stubs that stand in for the programme that delivers a requested procedure are compiled into programmes that employ Remote Procedure Calls (RPC).

A stub is a manageable replacement for a system collaborator or dependent that already exists. You can test your code without explicitly addressing the dependency by using a stub.

View more FAQs


What's up with Turing? Get the latest news about us here.


Know more about remote work.
Checkout our blog here.


Have any questions?
We'd love to hear from you.

Hire remote developers

Tell us the skills you need and we'll find the best developer for you in days, not weeks.

Hire Developers