When trying to test singleton itself, a solution may be possible:
public class Singleton { private static Singleton _Instance; public static Singleton getInstance() { if (_Instance == null) { _Instance = new Singleton(); } return _Instance; } private Singleton() { } public static resetForTesting() { _Instance = null } }
So, in your unit testing module, you should call Singleton.resetForTesting() before each unit test.
Note The disadvantage of this approach is the lack of a code level restriction that would prohibit anyone from invoking this method in production code, although it is intended only for use with test code. Therefore, you will have to rely on the documentation to pass this on to other people.
Sid
source share