I have a program that relies on an XML file to retrieve data from. When testing, I would like it to use a different XML file.
I would like to do this using ClassLoader, where the code may be similar to something along the lines of this:
ClassLoader loader = Thread.currentThread().getContextClassLoader();
Using loader, I would load the testing XML resource file. So all operations using the default XML file would instead be using the testing XML file.
How would I go about doing this?
You have different options for this, you can create different jar-files e.g. testing.jar and production.jar and put the corresponding file into these files. Get an InputStream with
Or you create a helper class, which is searching a File first, then trying to get the InputStream from the ClassLoader. If the file (with the right path) is available in your development environment you get the testing XML, in production environment the file should not be available so you get the production XML from the resource bundle (jar).
If you are using Maven, you have different resources (src/main/resources and src/test/resources) available.