Stub vs. Mock - Top Differences You Must Know

5 min read

  • Software comparisons

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.

Style

Stub vs. Mock: State testing vs. Behavioral testing

Principle

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.

Lifecycle

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.

Execution

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

Usage

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

Conclusion

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.

Share this post