Interfaces are cool

Interfaces are cool

I'm talking about OO interfaces. They came in handy recently when I was working on some code (Java) that launched a web browser as the result of a message that was received. In my test I was primarily concerned with the message I would receive back from that code.

So, the answer for me, was to extract the interface that I needed for the call to the web browser. Then I created a "mock" web browser that didn't do anything. So, when my tests are run the functional code around the call to the web browser is exercised and I don't have a bunch of open web browsers on my system.

You can do something similar if you are wanting to do some processing on the content received from a web page. Actually going out to the internet and pulling information is an expensive operation. Instead you can create a "mock" web browser class that has hard coded the expected web responses.

I've also been working on some C# code that involves communication between applications. By using an interface specification for the communications layer I can swap out the communications layer at will. For testing I'm using a mock object that implements the interface. Now I'm able to experiment with a variety of technologies for implementing the communications layer and I can choose the one that works best. Or I'll be able to choose the one that works best for a given situation. Definately cool stuff.

In summary, interfaces are a useful tool to keep in your bag of TDD tools.

Leave a Reply

Your email address will not be published. Required fields are marked *