Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating Mocks for Testing Application using Sockets for Data

 
Tarun Khandelwal
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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.

Thanks,
Tarun Khandelwal
 
Tim Cooke
Sheriff
Pie
Posts: 2920
121
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

You have:
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

Hope that helps
Tim
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic