JUnit and Singletons
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Dirk_Ludwig
Posted On:   Saturday, June 1, 2002 10:25 AM

Hello, I have a problem testing a class (say A) that uses an other class which is implemented as a Singleton. Thereby the singleton class is responsible for holding the current configuration of the application. The configuration itself is defined in a property file which is loaded by the Singleton-class as a Java resource each time a new instance of the Singlton is created (i.e. only once per JVM). Which property file is to be loaded by the singleton class is determined by a system property. The problem now is that I want to test how class A behaves using different configurations. So I have created one property file for each test case and set the system property for each configuration property file within the corresponding    More>>

Hello,


I have a problem testing a class (say A) that uses an other class which is implemented as a Singleton. Thereby the singleton class is responsible for holding the current configuration of the application. The configuration itself is defined in a property file which is loaded by the Singleton-class as a Java resource each time a new instance of the Singlton is created (i.e. only once per JVM). Which property file is to be loaded by the singleton class is determined by a system property.


The problem now is that I want to test how class A behaves using different configurations. So I have created one property file for each test case and set the system property for each configuration property file within the corresponding test-method.


It's no suprise that only the configuration for the first test method gets loaded, since in the context of the following test methods the Singleton has already been created.


My question now is: Is there a way to tell the JUnit TestRunner (text version) to reload classes anyway?


Thanks for your help in advance.


Dirk

   <<Less

Re: JUnit and Singletons

Posted By:   Dave_Astels  
Posted On:   Wednesday, June 12, 2002 02:04 AM

You may be better off getting rid o fthe singleton (which is often overused and improperly used). I suggest you read following paper from XP2002: http://www.xp2002.org/atti/Freeman-Simmons--Retrofittingunittests.pdf

Re: JUnit and Singletons

Posted By:   Sebastien_Meric  
Posted On:   Tuesday, June 4, 2002 10:59 PM

Hello,

Hopfully java won't let you reload a singleton paterned class.

What you may want then is make it possible to reconfigure your context class loader. You coul'd see Avalon framework to see if it's of any use for you.


Regards

Re: JUnit and Singletons

Posted By:   Anonymous  
Posted On:   Tuesday, June 4, 2002 12:26 AM

AFAIK there is no easy way to do that.

You should probably add a method to your singleton to load a new configuration for testing purposes. If you feel bad putting it into your official interface, you can encapsulate it into a nested class.

About | Sitemap | Contact