{"id":27197,"date":"2025-04-04T09:58:56","date_gmt":"2025-04-04T09:58:56","guid":{"rendered":"https:\/\/pl.server2.purelogics.net\/?p=27197"},"modified":"2025-04-04T11:34:32","modified_gmt":"2025-04-04T11:34:32","slug":"stub-vs-mock","status":"publish","type":"post","link":"https:\/\/pl.server2.purelogics.net\/?p=27197","title":{"rendered":"Stub vs Mock: Understanding the Difference in Unit Testing"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_65 counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/pl.server2.purelogics.net\/?p=27197\/#What_is_a_Stub\" title=\"What is a Stub?\">What is a Stub?<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/pl.server2.purelogics.net\/?p=27197\/#Stub_Example_in_Unit_Testing\" title=\"Stub Example in Unit Testing\">Stub Example in Unit Testing<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/pl.server2.purelogics.net\/?p=27197\/#Use_Cases_of_Stubs\" title=\"Use Cases of Stubs\">Use Cases of Stubs<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/pl.server2.purelogics.net\/?p=27197\/#What_is_a_Mock\" title=\"What is a Mock?\">What is a Mock?<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/pl.server2.purelogics.net\/?p=27197\/#Mock_Example_in_Unit_Testing\" title=\"Mock Example in Unit Testing&nbsp;\">Mock Example in Unit Testing&nbsp;<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/pl.server2.purelogics.net\/?p=27197\/#Use_Cases_of_Mocks\" title=\"Use Cases of Mocks\">Use Cases of Mocks<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/pl.server2.purelogics.net\/?p=27197\/#Stub_vs_Mock_in_Unit_Testing\" title=\"Stub vs Mock in Unit Testing\">Stub vs Mock in Unit Testing<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/pl.server2.purelogics.net\/?p=27197\/#Final_Remarks\" title=\"Final Remarks\">Final Remarks<\/a><\/li><\/ul><\/nav><\/div>\n<span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">3<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span>\n<p class=\"wp-block-paragraph\">Unit testing is a cornerstone of software development that ensures individual components of an application work as intended. However, one common challenge that software developers often face is \u2018dependencies.\u2019 When testing a module, external systems, APIs, or even other modules may interfere which makes it really difficult to isolate and test the code\u2019s functionality.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To solve this challenge of dependencies, we use test doubles like stubs and mocks. But do you know the difference between stub and mock in unit testing? Understanding the difference between the two is extremely critical for effective unit testing and boosting code reliability.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In today\u2019s blog post, we will bring stub vs mock differences under limelight, explore their use cases, and highlight their differences in unit testing. After reading this blog post, you\u2019ll understand how and when to use these tools in your testing strategy. So, without further ado, let\u2019s start reading!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_a_Stub\"><\/span><strong>What is a Stub?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A stub is a test double that provides predefined responses to method calls during a unit test. Stubs are used to isolate the system under test (SUT) by replacing its dependencies with simple implementations that mimic their behavior. They help simulate various scenarios without relying on actual implementations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Stub_Example_in_Unit_Testing\"><\/span><strong>Stub Example in Unit Testing<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Consider a shopping cart application. The Cart class relies on a ProductService to fetch product details. Instead of calling the real ProductService in your unit test, you can use a stub:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">class ProductServiceStub:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;def get_product_details(self, product_id):<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;id&#8221;: product_id,<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;name&#8221;: &#8220;Test Product&#8221;,<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;price&#8221;: 100<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"># In the test<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">product_service_stub = ProductServiceStub()<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">cart = Cart(product_service=product_service_stub)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">cart.add_product(&#8220;123&#8221;)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">assert cart.total_price == 100<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, you can see that the ProductServiceStub gives a fixed response for get_product_details which eliminates the need to connect to a database or API.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Use_Cases_of_Stubs\"><\/span><strong>Use Cases of Stubs<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The following are some top use cases of stubs in testing:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Simulating external dependencies:<\/strong> Stubs help test how a system behaves when external services, like APIs or databases, return specific data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Testing edge cases:<\/strong> They allow developers to test edge cases, such as timeouts or empty responses. These test edge cases might be difficult to reproduce with real dependencies.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Improving test performance:<\/strong> Stubs eliminate delays caused by network or database interactions. This makes testing quicker and more reliable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_a_Mock\"><\/span><strong>What is a Mock?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A mock, like a stub, is a test double, but it goes a step further. Mocks not only provide predefined responses but also record interactions. These include the number of method calls, the arguments passed, and the sequence of operations. They are used to verify behavior and ensure that specific methods are called as expected during a test.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Mock_Example_in_Unit_Testing\"><\/span><strong>Mock Example in Unit Testing&nbsp;<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s extend the shopping cart example. Suppose the Cart class sends a notification via NotificationService when a product is added. You can mock the NotificationService to verify this behavior:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">from unittest.mock import Mock<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"># Mock the NotificationService<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">notification_service_mock = Mock()<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">cart = Cart(notification_service=notification_service_mock)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">cart.add_product(&#8220;123&#8221;)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"># Verify behavior<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">notification_service_mock.send_notification.assert_called_once_with(&#8220;Product added: Test Product&#8221;)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here, the mock ensures that the send_notification method is called exactly once with the specified argument.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Use_Cases_of_Mocks\"><\/span><strong>Use Cases of Mocks<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now, let\u2019s have a look at a few use cases of mocks in unit testing!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Behavior verification:<\/strong> Mocks are ideal for verifying that specific methods are called with the correct parameters.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Simulating complex interactions:<\/strong> They are useful when testing interactions between multiple components, ensuring they follow the desired sequence.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Validating side effects:<\/strong> Mocks help ensure side effects, like database writes or email sends, occur as expected during a test.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Stub_vs_Mock_in_Unit_Testing\"><\/span><strong>Stub vs Mock in Unit Testing<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Understanding the difference between stubs and mocks is crucial for writing effective tests. Look at the table below for a detailed comparison:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Feature<\/strong><\/td><td><strong>Stub<\/strong><\/td><td><strong>Mock<\/strong><\/td><\/tr><tr><td><strong>Purpose<\/strong><\/td><td>Provides predefined outputs<\/td><td>Verifies interactions with methods<\/td><\/tr><tr><td><strong>Focus<\/strong><\/td><td>Data state and test isolation<\/td><td>Behavior and interaction testing<\/td><\/tr><tr><td><strong>Implementation Complexity<\/strong><\/td><td>Relatively simple<\/td><td>More complex with verification logic<\/td><\/tr><tr><td><strong>Test Dependency<\/strong><\/td><td>Simulates data responses<\/td><td>Monitors and validates calls<\/td><\/tr><tr><td><strong>Use Case<\/strong><\/td><td>Replace external systems<\/td><td>Validate method calls<\/td><\/tr><tr><td><strong>Verification<\/strong><\/td><td>No verification of interactions<\/td><td>Tracks and verifies method usage<\/td><\/tr><tr><td><strong>Performance Impact<\/strong><\/td><td>Improves test speed by isolating<\/td><td>Adds overhead due to interaction tracking<\/td><\/tr><tr><td><strong>Typical Tools<\/strong><\/td><td>Custom-coded or simple frameworks<\/td><td>Mocking libraries like Mockito, Moq<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Final_Remarks\"><\/span><strong>Final Remarks<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Both stubs and mocks are indispensable tools in a developer\u2019s testing arsenal. While stubs focus on simplifying dependencies and simulating external systems, mocks take it further by verifying behavior and interactions. Knowing when to use a stub versus a mock can significantly enhance the effectiveness of your unit tests and improve code quality.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By understanding the technical nuances and realistic applications of these tools, developers can write more robust tests, reduce bugs, and ensure smoother deployments. The key lies in identifying the purpose of your test: isolate the data? Use a stub. Verify behavior? Go for a mock.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, the next time you write a unit test, ask yourself: Am I testing data or behavior? The answer will guide you to the right choice between a stub and a mock. For more details, <a href=\"https:\/\/pl.server2.purelogics.net\/contact-us\/\">get in touch<\/a> with PureLogics.<\/p>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\">3<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span> Unit testing is a cornerstone of software development that ensures individual components of an application work as intended. However, one common challenge that software developers often face is \u2018dependencies.\u2019 When testing a module, external systems, APIs, or even other modules may interfere which makes it really difficult to isolate and test the code\u2019s functionality. To <a href=\"https:\/\/pl.server2.purelogics.net\/?p=27197\" class=\"more-link\">&#8230;<\/a><\/p>\n","protected":false},"author":11,"featured_media":27199,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[548],"tags":[],"class_list":["post-27197","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-quality-assurance"],"_links":{"self":[{"href":"https:\/\/pl.server2.purelogics.net\/index.php?rest_route=\/wp\/v2\/posts\/27197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pl.server2.purelogics.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pl.server2.purelogics.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pl.server2.purelogics.net\/index.php?rest_route=\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/pl.server2.purelogics.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=27197"}],"version-history":[{"count":1,"href":"https:\/\/pl.server2.purelogics.net\/index.php?rest_route=\/wp\/v2\/posts\/27197\/revisions"}],"predecessor-version":[{"id":27200,"href":"https:\/\/pl.server2.purelogics.net\/index.php?rest_route=\/wp\/v2\/posts\/27197\/revisions\/27200"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pl.server2.purelogics.net\/index.php?rest_route=\/wp\/v2\/media\/27199"}],"wp:attachment":[{"href":"https:\/\/pl.server2.purelogics.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=27197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pl.server2.purelogics.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=27197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pl.server2.purelogics.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=27197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}