Creating Mocks for Testing Application using Sockets for Data
posted 2 years ago
I have to test my application.
It uses data from another application through Socket Connection.
I don't want to install this application, So how can i mock the Socket Service easily.
One way that i know is to create a local service that will work same as actual service by creating local ServerSocket.
Is there any way i can do that, just like mocking db connections, so that i don't have to take care of synchronizing Sockets and there Streams.
I would wrap the code in your application that deals with connecting to this external application using an Adaptor/Wrapper [GoF: Design Patterns]. By doing so then your main application domain code can integrate with the interface of your adaptor rather than the Socket stuff directly which means you are then free to swap the adaptor out with a Test Double when testing.
So instead of this sort of system architecture:
Application Domain --(Socket Connection)--> External Application
Application Domain --> ExternalApplicationAdaptor --(Socket Connection)--> External Application
The idea is that for testing you can swap out the ExternalApplicationAdaptor with a Test Double that does not depend on the external application and that you have full control over.
Application Domain --> ExternalApplicationAdaptorTestDouble
You can now manipulate the ExternalApplicationAdaptorTestDouble to behave in whatever manner you require to test the behaviour of your ApplicationDomain