Working with easymock

Redchilliplus's Blog

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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: