Showing posts with label singleton. Show all posts
Showing posts with label singleton. Show all posts

Sunday, March 1, 2009

Mocking singletons and Factories

Mocking singletons ., factories may not be that obvious.

public class Singleton {
private Singleton instance = new Singleton ();
public static Singleton getInstance() {
return instance;
}
}

@Test
public void testSingleton() {
Field instanceField = Singleton .class.getField("instance");
instanceField.setAccessible(true);
Singleton realSingleton = instanceField.get(null);
Singleton mockSingleton = EasyMock.createMock(Singleton .class);
try {
instanceField.set(null, mockSingleton);
// now do your testing, Singleton.getInstance() should now return the mock
} finally {
instanceField.set(null, reallSingleton);
instanceField.setAccessible(false);
}
}

This is fairly hacky, not robust to changes in the singleton class, won't work with a security manager, and it may not work well in the case where the field is instantiated in a static initializer and other fields use that instance when they are initialized. But it probably will work in many cases.