Software QA: Mocking and Stubbing

Mohamad Rifqy Zulkarnaen
4 min readJun 7, 2021

In art of software quality assurance, there are many techniques and implementations that we must know in order to create good tests so that we can validate our working software with easy. One of the most commonly used techniques in developing tests is using Mocks and Stubs. What are these techniques? How does it work? And what can we get from using it? Let’s talk about it deeper in this article!

Source image: https://medium.com/@makersinstitute

First of all we should get to know what is mocks and stubs. After that we’re going to see the example of mocks and stubs. Let’s dive deeper into it!

Mocks

Mocks are fake classes that created for testing an internal or external services or objects. We usually can found mocks in unit tests since we may found many function and classes depends on each other. These mocks or fake classes will simulate the behavior of the target service as if it’s the original one. It helps the test to isolate behavior of a function or classes so that we can only focused on the method that we tested without getting any interfere from other classes.

Stubs

Stubs are classes that returns data from its target method and function. Usually the data that returned are hard-coded by the one who made the tests. We can say that it’s similar to mocks, but it is more focused on one method or function rather than the entire object itself. Stubs also commonly used on unit test to check whether a function or method returns a correct output.

Examples

Here are the examples of mocks and stubs in my group project, SIMPK Mobile App (Project for PPL Course 2021). I hope it helps!

Mocks example

It’s one of mocks that I implemented on creating unit tests for Bukti Laporan API (an API for create and read File for monitoring purpose). We can see that I created a mock class for one of Django File class. The purpose of this mock class is to become fake file classes so that it can be accepted by the API endpoint that I made before. If I delete the mock File class, the API that I created cannot accept the request because it will detect the file as not a File class.

And for another example, check this out:

Stubs example
Another stubs example

The first image is a test that I made for testing Booking tab after it’s called API endpoint to get the corresponding booking data. We can see at line 60–69 I tried to stub the object by telling its output when it calls the API using getAllBooking method.

The second image is a test that I created to check whether my newly created API call function in frontend (Flutter) returns correct output. At line 360–373, we can see that I tried to use stubs to provide behavior for MockClient class so that when MockClient call the API, it will returns JSON response with status code 200.

Thoughts and Conclusion

Personally I think that mocks and stubs is quite easy to use since we just focus on behavior of a classes or method. We can easily see how they perform and we can evaluate it based on our input. We also can not to worry how other classes behavior will affect the output of our testcase because mocks and stubs will isolate them.

But if we’re new to the software QA world, I think it’s not easy for beginner tester to understand these techniques. Not many sources that can help us to understand how these techniques works. We may found many questions and its solution on programming forum regarding mocks and stubs, but usually our case and their case are different so that how they implement the mock and stubs is different too in our apps. Therefore,

--

--

Mohamad Rifqy Zulkarnaen

just your typical curious and storytelling loving software engineer.