Open In App

Software Testing – Mock Testing

Mock testing is the procedure of testing the code with non-interference of the dependencies and different variables like network problems and traffic fluctuations making it isolated from others. The dependent objects are replaced by mock objects which simulate the behavior of real objects and exhibit authentic characteristics. The motto of mock testing is to focus on testing without prioritizing the dependencies. Here, we will discuss the following topics:

  1. Where is Mock Testing Useful?
  2. Examples of Mock Testing
  3. How Does Mock Test Work?
  4. Does Mocking Require Developers To Modify Their Codebase?
  5. Mock Testing vs Traditional Unit Testing
  6. Types of Mock Testing
  7. Best Practices For Mock Testing
  8. Mocking Frameworks
  9. Precautions During Mock Testing
  10. Advantages of Mock Testing
  11. Limitations of Mock Testing

Let’s start discussing each of these topics in detail.



Where is Mock Testing Useful?

Here are some of the scenarios where mock testing is useful.

Examples of Mock Testing

Let’s take an example of Faking API calls.



How Does Mock Test Work?

It’s an approach to unit testing that enables the creation of assertions concerning how the code behind the test is interacting with alternative system modules. 

Does Mocking Require Developers To Modify Their Codebase?

The mocking does not require modifying the codebase. The mock object simply reads and responds with test data from a local filesystem. 

Unlike traditional unit testing, the assertion is done by mock objects which are initialized in advance with respect to what method calls are expected and how they should respond. Mocking is used for protocol testing in which it tests how to use API and how it will react to API implemented accordingly.

Mock Testing vs Traditional Unit Testing

Below are some of the differences between mock testing and traditional unit testing:

Mock Testing

Traditional Unit Testing

In mock testing, assertions are done by mock objects and no assertions are required from unit testing While in traditional unit testing assertions are done for dependencies.
In mock testing, it is focused on how the fake/mock object can be incorporated and tested. In traditional unit testing, it was focused on how the real object can be incorporated and tested.
Mock testing is more about behavior-based verification. Traditional unit testing is more about state-based verification.

Types Of Mock Testing

  1. Classloader-remapping-based mocking: In this the reference is remapped by the class-loader so the mock object is loaded rather than the original object.
  2. Proxy Based Mocking: In this a proxy object is used rather than an original proxy object which handles all calls to original objects.
  3. Database Based Mocking: In a database-based mocking, the user does not perform the actual Database operation rather the user will replace the operation with a mock object to validate the functionality.
  4. API Based Mocking: In API-based mocking API mocks are used to simulate external dependencies and unexpected behavior. 

Best Practices For Mock Testing

Mocking Frameworks

Mocking frameworks are accustomed to generating replacement objects like Stubs and Mocks. Mocking frameworks complement unit testing frameworks by eradicating dependencies however don’t seem to be substituted for unit testing frameworks. By erasing the dependencies, they assist the unit testing method and aid developers in writing a lot of targeted and pithy unit tests. The mock tests conjointly perform quicker by really making the system isolated underneath the test.
Some mocking frameworks are :

  1. Using EasyMock: It is an open-source framework that provides useful classes for mocking objects. It creates mock objects of classes and interfaces using the statically imported methods of the class extensions.
  2. Using JMock: It doesn’t rely on any static import features. It mocks interfaces only. JMock library is easy to use and provides better integration with Junit5. 
  3. Using Mockito: It also doesn’t rely on static import features. It creates mock objects through annotations. Thus it is used to register extensions for the annotated test class or test method.

Precautions During Mock Testing

  1. Only Mock the Type That Is Just Owned: External sorts have dependencies on their own they could even modify their behavior at a later stage. A better approach is to produce an associate adapter and mock that adapter.
  2. Don’t Mock Values: Values mustn’t be mocked. Mocking aims to create the relationships and interactions between visible objects. Obtaining a value isn’t associated with the interaction.
  3. Avoid Mocking Concrete Categories: Relationships are made to view via interfaces easier instead of concrete classes. we’d find mocking some methods however forget others. Mock Roles, not objects.
  4. Don’t Mock Everything: This is often associated with anti-patterns. If everything is mocked, we have a tendency to might find testing one thing quite totally different from the production system.
  5. Use Integration Tests: Once testing multiple modules or if curious about information coming back from associate external information, integration testing must be done instead of mocking.
  6. Negative Tests: Mocks should be used to simulate errors and test error handling as well as to verify some other methods.

Advantages Of Mock Testing

Limitations Of Mock Testing


Article Tags :