I'm writing a test case that loads variables from the resources of my tests and loads them into a Properties object. It checks if there is a type of file that only exists in a Docker container when it is generated by configuration related to the Docker container. Let me be clear: config.alt.properties
doesn't currently exist in my file system, but it used to and I believe I once ran these tests while it did exist. When I run this within my eclipse environment, I found out via a Debugger that the ContextClassLoader
pulls the file out of thin air, and loads it with the values that were once in that file, but different from the normal config.properties
file.
props = new Properties();
try (InputStream altInput = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.alt.properties")) {
if(altInput != null){
props.load(altInput);
}
else{
LOG.info("Using environment props file");
InputStream envInput = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties");
props.load(envInput);
}
} catch (IOException e){
e.printStackTrace();
}
Why is this happening? And more importantly, how do I get the ContextClassLoader to forget about this file that isn't here anymore? Here is my project structure:
src/main/java: {empty}
src/main/resources: rebel.xml
src/test/java: testClass.Java (where code is from)
src/test/resources: config.properties, log4j2.xml
I have tried to use the other methods of resource loading mentioned here but all of the other methods lead to the program not finding any files at all including config.properties
.
Any help is appreciated, thank you.
EDIT: The tests are running using JUnit, I don't know if that matters or not. Here is the JUnit info in the pom.XML
Under dependencies:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
and under dependencyManagement
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>