1.

Why do we need mocking in unit testing?

Answer»

MOCKING is the process of creating and using mock objects that simulates the behaviour of real objects and is used to isolate the behaviour of the module under test from its external dependencies or services. These are particularly useful in unit testing and help in making unit test cases repeatable and PREDICTABLE. Mocking is required in the following cases:

  • Whenever the module under test has dependencies that are not fully implemented. For example, if we have a module that calls a REST API that is still in progress, then to test our module, it is advised to mock the API call and perform unit testing of our module.
  • Whenever the module under test updates the SYSTEM states. For example, whenever the module involves database calls that creates or updates or delete data from the system, it is very much IMPORTANT to mock those objects.
  • Even if we have DB calls that just retrieves the data, it is advised to mock those because of the risk of DB availability.


Discussion

No Comment Found