Categories: Unit Testing

Unit Tests – Mocking vs Stubbing Vs Spying

The article lists down some good reads (from different webpages on internet) on unit testing concepts such as mocking, stubbing and spying and represents summarized information on these concepts.

After going through all the above pages, I could arrive at following definitions:

Mocking

Mocking is a unit testing phenomenon which is used to replace real objects with test objects that mocks the behavior. These test objects are called as Mock objects. The point to note is that mock objects are not coded in advance with pre-determined return results. On the other hand, they are created at run-time with set expectations which are later verified after the execution of the tests. Tests written with mock objects follow following pattern:

  • Initialize mocks
  • Set expectations
  • Execute the tests
  • Verify whether one or more methods got executed as expected

The primary reason for using mocking is to eliminate testing all the dependencies of a class or function so your tests are more focused and simpler.

 

Stubbing

Stubbing, on the other hand, is used to replace real objects with test objects that are coded in advance with predefined behavior and return canned results. Those working with dependency injection frameworks such as Spring finds it very easy to inject stubs in place of real objects while doing unit testing. Stubs may also be used to record some data around testing which is not possible with mock objects. This is also one of the key reason why one may want to go with stubbing in some cases rather than mocking. However, unlike mock objects, Stubs could not be used to verify interactions and related behavior.

Spying

Spying is a mocking concept which is used to do partial mocking. Spying is used in the cases when there is a need to mock only few methods of the class, primarily due to the reason that execution of these methods would have one or more side effects. Spying is recommended mainly in the case of legacy code.

[adsenseyu1]
Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.

Share
Published by
Ajitesh Kumar
Tags: Unit Testing

Recent Posts

Retrieval Augmented Generation (RAG) & LLM: Examples

Last updated: 25th Jan, 2025 Have you ever wondered how to seamlessly integrate the vast…

1 month ago

How to Setup MEAN App with LangChain.js

Hey there! As I venture into building agentic MEAN apps with LangChain.js, I wanted to…

2 months ago

Build AI Chatbots for SAAS Using LLMs, RAG, Multi-Agent Frameworks

Software-as-a-Service (SaaS) providers have long relied on traditional chatbot solutions like AWS Lex and Google…

2 months ago

Creating a RAG Application Using LangGraph: Example Code

Retrieval-Augmented Generation (RAG) is an innovative generative AI method that combines retrieval-based search with large…

2 months ago

Building a RAG Application with LangChain: Example Code

The combination of Retrieval-Augmented Generation (RAG) and powerful language models enables the development of sophisticated…

2 months ago

Building an OpenAI Chatbot with LangChain

Have you ever wondered how to use OpenAI APIs to create custom chatbots? With advancements…

2 months ago