-1

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>
littlespice3
  • 131
  • 1
  • 2
  • 12

1 Answers1

1

You said the file does not exist in filesystem, but it seems you only looked for it under the sources directory. But at runtime the program uses resource files under target directory (they are copied there at build-time, at "process-resources" phase of default lifecycle), so the only explanation I see, and quick solution is to mvn clean (or use "Clean project" feature in Eclipse).

To verify where the file is taken from, right before you get it as stream, try Thread.currentThread().getContextClassLoader().getResource("‌​config.alt.propertie‌​s"), log the value, and verify the location it points to.

Hugues M.
  • 19,846
  • 6
  • 37
  • 65
  • mvn clean/reinstall worked to clear it out. I wasn't thinking about the maven aspect of the project and test as much as I should have. Thank you. – littlespice3 Jul 07 '17 at 18:31