lundi 29 juin 2015

Dependency injection for unit testing vs. class managing its own variables

I have a class that is something like this (very simplified but this is the general idea. I'm not actually using the raw socket type):

class ConnectionBroker {

    private Socket socket;

    public ConnectionBroker(ConnectionDetails details) {
        socket = new Socket(details)
        //set socket paramaters
    }

    public sendOverConnection(String message) {
        //processing of message and internal queuing based on priority
        socket.send(message);
    }

    public shutdown() {
        socket.close();
    }
}

It would be valuable if I could unit test the class's functions by mocking the socket variable to check what is being sent over the socket. The standard way of doing this that I have seen is modifying the constructor to be

public ConnectionBroker(Socket socket) {
    this.socket = socket;
}

This would make unit testing a lot easier, however I have a number of issues with doing it this way:

  • The socket should be maanged only by the ConnectionBroker class
  • The fact that the ConnectionBroker class uses sockets at all should be abstracted away from the calling class
  • If the socket needs to be passed in then the calling code could possibly modify attributes on the socket which the the ConnectionBroker class is not aware of

What is the proper way of solving this? Am I thinking of something incorrectly?

Aucun commentaire:

Enregistrer un commentaire