Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

I have a factory class to retrieve the configuration for my application:

public class ConfigurationFactory { private static ConfigurationFactory configurationFactory = new ConfigurationFactory(); private Configuration configuration = null; public static ConfigurationFactory getConfigurationFactory() { return configurationFactory; } 

And some getConfiguration methods depending on where I am getting the config from (file, database, default, etc...):

public Configuration getConfiguration(String path){...} public Configuration getConfiguration(String database){...} 

My problem is that when I do unit test for each method, I have to restart the singleton in order to load from a different source, so the only thing I had come up to is adding this:

public void resetConfiguration() { this.configuration = null; } 

And I feel I'll burn in developer's hell for doing it :)

My question: Is there any other way to do it without adding this method?

Note: I have seen thisthis and I cannot use any DI framework like Spring or Guice, management thinks adding a framework would make the project heavyweight, as this program is intended to run as a daemon on servers.

I have a factory class to retrieve the configuration for my application:

public class ConfigurationFactory { private static ConfigurationFactory configurationFactory = new ConfigurationFactory(); private Configuration configuration = null; public static ConfigurationFactory getConfigurationFactory() { return configurationFactory; } 

And some getConfiguration methods depending on where I am getting the config from (file, database, default, etc...):

public Configuration getConfiguration(String path){...} public Configuration getConfiguration(String database){...} 

My problem is that when I do unit test for each method, I have to restart the singleton in order to load from a different source, so the only thing I had come up to is adding this:

public void resetConfiguration() { this.configuration = null; } 

And I feel I'll burn in developer's hell for doing it :)

My question: Is there any other way to do it without adding this method?

Note: I have seen this and I cannot use any DI framework like Spring or Guice, management thinks adding a framework would make the project heavyweight, as this program is intended to run as a daemon on servers.

I have a factory class to retrieve the configuration for my application:

public class ConfigurationFactory { private static ConfigurationFactory configurationFactory = new ConfigurationFactory(); private Configuration configuration = null; public static ConfigurationFactory getConfigurationFactory() { return configurationFactory; } 

And some getConfiguration methods depending on where I am getting the config from (file, database, default, etc...):

public Configuration getConfiguration(String path){...} public Configuration getConfiguration(String database){...} 

My problem is that when I do unit test for each method, I have to restart the singleton in order to load from a different source, so the only thing I had come up to is adding this:

public void resetConfiguration() { this.configuration = null; } 

And I feel I'll burn in developer's hell for doing it :)

My question: Is there any other way to do it without adding this method?

Note: I have seen this and I cannot use any DI framework like Spring or Guice, management thinks adding a framework would make the project heavyweight, as this program is intended to run as a daemon on servers.

Source Link
Eugenio Cuevas
  • 11.1k
  • 3
  • 33
  • 51

Test singleton with jUnit

I have a factory class to retrieve the configuration for my application:

public class ConfigurationFactory { private static ConfigurationFactory configurationFactory = new ConfigurationFactory(); private Configuration configuration = null; public static ConfigurationFactory getConfigurationFactory() { return configurationFactory; } 

And some getConfiguration methods depending on where I am getting the config from (file, database, default, etc...):

public Configuration getConfiguration(String path){...} public Configuration getConfiguration(String database){...} 

My problem is that when I do unit test for each method, I have to restart the singleton in order to load from a different source, so the only thing I had come up to is adding this:

public void resetConfiguration() { this.configuration = null; } 

And I feel I'll burn in developer's hell for doing it :)

My question: Is there any other way to do it without adding this method?

Note: I have seen this and I cannot use any DI framework like Spring or Guice, management thinks adding a framework would make the project heavyweight, as this program is intended to run as a daemon on servers.