Working with easymock
Like it or not, you will find your class using some legacy singleton code. Problem begins when you try to unit test your class. Lets dive into some example and see how to write unit test for such a class.
Singleton.java is typical singleton implementation. Our new class ClassUsingSingleton.java is using Singleton.
Lets assume that the ClassUsingSingleton.java is user for the Singleton.
If you try to run following Junit test, it will throw exception because Singleton will try to load file from some relative path which is not available in your unit test.
Problem here is Singleton because it is trying to load properties from a defined location, in real world it may be getting database connection, JMS connection or having remote reference of an EJB. With all these kind of initialization in private constructor, we can not unit test singleton with simply using mock object.
To unit test, we…
View original post 166 more words