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.
Some of the things which i can think of are
Use mocking frameworks like
EasymockorMockitoto mock theConfigurationFactoryand use this mock objects in other calling areas.Have a setter defined in the
ConfigurationFactoryclass and define an annotation like@TestPurposeand use this to override the singleton object from your unit tests. The annotation is to denote that this should not be used during the application flow and the function is meant only for junit purpose.