Thursday, May 15, 2008

TotT: Using Dependancy Injection to Avoid Singletons

It's hard to test code that uses singletons. Typically, the code you want to test is coupled strongly with the singleton instance. You can't control the creation of the singleton object because often it is created in a static initializer or static method. As a result, you also can't mock out the behavior of that Singleton instance.

If changing the implementation of a singleton class is not an option, but changing the client of a singleton is, a simple refactoring can make it easier to test. Let's say you had a method that uses a Server as a singleton instance:

public class Client {
  public int process(Params params) {
    Server server = Server.getInstance();
    Data data = server.retrieveData(params);
    ...
  }
}


You can refactor Client to use Dependency Injection and avoid its use of the singleton pattern altogether. You have not lost any functionality, and have also not lost the requirement that only a singleton instance of Server must exist. The only difference is that instead of getting the Server instance from the static getInstance method, Client receives it in its constructor. You have made the class easier to test!

public class Client {
  private final Server server;

  public Client(Server server) {
    this.server = server;
  }

  public int process(Params params){
    Data data = this.server.retrieveData(params);
    ...
  }
}


When testing, you can create a mock Server with whatever expected behavior you need and pass it into the Client under test:

public void testProcess() {
  Server mockServer = createMock(Server.class);
  Client c = new Client(mockServer);
  assertEquals(5, c.process(params));
}


Remember to download this episode of Testing on the Toilet and post it in your office.

6 comments:

  1. This doesn't cover a few downfalls to the approach.

    It requires the users of the client know about where to get the singleton and it forces them to access it for every creation of the client class.

    Another problem is that the singleton that this ignores the difference in when the singleton is acquired, which could have important implications that need to be addressed.

    It should be included in such a suggestion that the client class take the singleton injection as an optional parameter, and access the singleton by default, possibly and specifically on each use of the singleton.

    ReplyDelete
  2. After several failures, we (I and my team in the undisclosed company I work for) got to couple of conclusions:
    1. DI frameworks (e.g. Spring) are extremely friendly for testing.
    2. If you can't use a DI framework, create (a simple) one.
    Now (2) requires some explanation – what we do is have one real singleton – The singleton (application context, container, god – take your pick) which acts as a very restricted access map to several contexts "hubs". Each hubs converse directly with the map, first loading it with the required objects, then extracting and typecasting as needed. Context objects have a very small footprint on construction and are only initialized on event (application init) / request (same as real singletons)
    Testing is facilitated in either of two ways:
    1. If you only need a small number of mock contexts, you create them and load them manually into the singleton map. Future requests will retrieve them. On tests teardown, you empty the entire map.
    2. If you need a large number of mock contexts (big unit tests / integration tests), you just fire one (or more) of the real hubs and overwrite only what's needed in the map (if anything is) with mock implementations.

    This approach has the merit of being very easy on production code and yet (almost) as testable as real DI.

    ReplyDelete
  3. To be honest, I do not see the advantage...

    With the first implementation of the Client-Class, it would have been possible to do:

    Client c = new Client();
    client.process();

    That's all... what's the drawback here?

    ReplyDelete
  4. It's hard to test code that uses singletons. << understatement of the year :P
    We have had alot of luck using DI for testing, and when singletons are necessary provide an alternative getInstance method which takes in the parameters to construct the object with for testing.

    ReplyDelete
  5. years after... :) well, but PHPUnit official documentation is still linking to this blog post so...

    This post is a great and clear explanation of DI benefit at time of unit testing code, but in any case does it demonstrate what problems singletons are causing when unit testing code.

    As said haemi, the first Client.process() method implementation could (not should :)) have been done like this:

    public int process(Params params) {
    Server server = new Server();
    Data data = server.retrieveData(params);
    ...
    }

    In this case, we don't have any singleton involved, but we still cannot unit test this method because of a hard dependency on Server class.

    Indeed, the biggest issue with singleton is to unit test it's different methods independently, since you're not supposed to be able to reset the instance between method calls.

    Using introspection (or, but it's kind of ugly, implementing a resetInstance() public method to delete the current instance and allow instantiating a new one), you can manage to unit test a singleton's methods.

    Tu sum up, dependency injection and singletons are not related in any way!

    ReplyDelete

The comments you read and contribute here belong only to the person who posted them. We reserve the right to remove off-topic comments.