Software Engineering | MOCK (Introduction)

Mock is an Object that clone the behavior of a real object. It is basically used in Unit Testing by testing the isolated unit even when Backend is not available.

Why we use MOCK object?
The unit testing purpose is to approve each unit of software designed and verify that the generated code is working perfectly, interdependent on external dependencies. Most of the cases, Code under test has some external dependencies like APIs and It would be better to create a mock object instead of generating test cases on the real object of the dependencies.

A web application consists of two components: Frontend and Backend server that are dependent on each other and run simultaneously.The developer of the Frontend is dependent on the backend developer for a server, APIs, and other external services. In the testing or development phase, a major challenge is to handle the various external dependencies.
Real Environment exchanges their data through a server where user-end services are handled by a different server and admin services are handled by another server. Developer facing difficulty while developing software. On the other hand tester not able to do efficient unit testing when software is dependent on external dependencies. In order to provide effective testing, mock server is required. Mock server cut out the dependency on a real server and allows tester to do testing independently. Figure gives virtual Representation of Mock server.



Now, in order to effectively utilizes our time and improve testing mechanism, there is a requirement of a Mock server that behaves as a real server, mimic its dependencies. Some of the mocking frameworks available to unit test Java application are Jmock, Mockito, EasyMock. For unit testing, mock objects can be used in place of real objects by simulating the Interfaces required. Mocks are easiest to use while Interface-based design systems.

There is some pattern to be followed for Unit testing with Mock objects:

  1. Build an Instance or object of Mock object.
  2. Define States of Mock objects under defined environment.
  3. Set expectations, status codes, responses, error display in mock object.
  4. Set Mock object as parameter under domain code.
  5. Verify Mock objects under unit testing.


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : SoniAnshu



Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.