I have a resource file which has some settings. I have a ResourceLoader class which loads the settings from this file. This class is currently an eagerly instantiated singleton class. As soon as this class loads, it reads the settings from the file (file path stored as a constant field in another class). Some of these settings are not suitable for unit tests. For example, I have thread sleep time in this file, which may be hours for production code, but I'd like it to be a couple of milliseconds for unit tests. So I have another test resource file which has a different set of values.
How do I go about swapping the main resource file with this test file during unit testing? The project is a Maven project and I'm using TestNG as the testing framework. These are some of the approaches I've been thinking about but none of them seem ideal:
Use
@BeforeSuite
and modify theFilePath
constant variable to point to the test file and use@AfterSuite
to point it back to the original file. This seems to be working, but I think because theResourceLoader
class is eagerly instantiated, there is no guarantee that the@BeforeSuite
method will always execute before theResourceLoader
class is loaded and hence old properties may be loaded before the file path is changed. Although most compilers load a class only when it is required, I'm not sure if this is a Java specification requirement. So in theory this may not work for all Java compilers.Pass the resource file path as a command line argument. I can add the test resource file path as command line argument in the surefire configuration in the pom. This seems a bit excessive.
Use the approach in 1. and make
ResourceLoader
lazy instantiated. This guarantees that if@BeforeMethod
is called before the first call toResourceLoader.getInstance().getProperty(..)
,ResourceLoader
will load the correct file. This seems to be better than the first 2 approaches, but I think making a singleton class lazy instantiated makes it ugly as I can't use a simple pattern like making it an enum and such (as is the case with eager instantiation).
This seems like a common scenario, what is the most common way of going about it?