Tests and Singletons: Singletons With Benefits!
So have you just created a nice shiny class implementing SingletonPattern and now you’ve got to test it? You may find to your horror that the singleton is messing up the tests!
This is an example of an [innocent] singleton class.
class Singleton
{
private Singleton instance;
public static Singleton Instance()
{
if(instance==null)
instance=new Singleton();
return instance;
}
private Singleton()
{
}
}
The problem is when a new instance of a Singleton class is created; that stays in memory for the lifetime of application, and since each unit test is supposed to be ran independently, this charactristics of Singleton class causes problems.
A solution is to “break” the pattern and add a new method for “resetting” the Singleton instance, and use that method only in unit tests.
Here’s the purposed solution
class Singleton
{
private Singleton instance;
public static Singleton Instance()
{
if(instance==null)
instance=new Singleton();
return instance;
}
public void Reset()
{
instance=new Singleton();
}
private Singleton()
{
}
}