I believe that what you're asking is not about "improving testability" but simply to "test" code sending things over the network.
From my experience, there are only two interesting things to test for things sent over the network:
- Is my application sending the right message?
- Am I able to send this message over the network?
For example, let's say that your application can send some (applicative-level) message to another server. You can define an interface like this one:
public interface MessagePort {
void Send(Message message, ServerName to);
}
Where Message is the message you want your application to send, and ServerName a class representing an applicative-level way to name the target of the message. For the sake of simplicity, ServerName can be a simple string that can be mapped to the address of the server by configuration, DNS, or whatever. You choose.
You can implement a MockMessagePort, storing the messages into a simple list, to test that your application is indeed sending the right message. (Question 1.)
After that, you can implement the actual sending of the message over the network by using the loopback. You may want to read Uncle Bob's "Craftsman" articles for an example of this.
http://www.objectmentor.com/resources/publishedArticles.html, topic "Craftsman". The test-driven implementation of a TCP server is some episodes away into the series, but you should read it from the start.
An even more interesting abstraction is that of a "Channel" to a message addressee.
public interface MessageChannel {
void Send(Message message);
}
Where the implementation knows about the addressee. Of course, in this case, there is another object providing the channel, from the addressee "ServerName".
If you really want to have an abstract model of the network facilities, well, here is what you can do:
- Try to find two different network libraries.
- Write the same app twice, using each of the libraries.
- Factor the apps. The applicative code should be the same, an the network part should vary. They will be separated by an interface. This is the interface you are looking for.
But honestly, I don't believe that it will bring much to your application.