2

I need to load from the test properties file instead of the main properties file while running the test. I have the following project structure

src
|--- main
|      |--- package/PropertiesLoader.java
|      |--- package/Main.java
|      |--- resources/myprop.properties
|
|--- test
|      |--- package/AppTest.java
|      |--- resources/myprop.properties

In my PropertiesLoader.java, I am loading my properties as

public class PropertiesLoader {

    public static Properties getApplicationProperties() {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

        String path = classLoader.getResource("myprop.properties").getPath();

        File file = new File(path);

        try (InputStream inputStream = new FileInputStream(file);
             InputStreamReader streamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8)) {

            // load the properties form stream reader
            Properties prop = new Properties();
            prop.load(streamReader);

            return prop;
        } catch (IOException e) {
            throw new RuntimeException("Cannot read properties file");
        }
    }
}

My Main class

public class Main {
    public static void main(String[] args) {
        Properties prop = PropertiesLoader.getApplicationProperties();
        System.out.println(prop.getProperty("program.value")); // expects main
    }
}

my AppTest.java tries to load its properties but fails

public class AppTest {

    @Test
    public void loadProp_test() {
        Properties prop = PropertiesLoader.getApplicationProperties();

        String value = prop.getProperty("program.value");
        assertEquals("test", value); // expects test
   }

}

This always loads main/resources/myprop.properties. How do I load test/resources/myprop.properties while running the test. I am not using spring or any other framework.

User45
  • 74
  • 1
  • 10
  • Welcome to Stack Overflow. Please take the [tour] to learn how Stack Overflow works and read [ask] on how to improve the quality of your question. Then [edit] your question to include the full source code you have as a [mcve], which can be compiled and tested by others. Include the unit tests as well and how you try to access the resources. Then add a detailed description on how you run the unit tests and where exactly you see that the wrong properties file is loaded. – Progman May 23 '21 at 19:33
  • Please check if this helps! https://stackoverflow.com/questions/3891375/how-to-read-a-text-file-resource-into-java-unit-test – Rao May 23 '21 at 19:40
  • You might want to check https://stackoverflow.com/questions/1771679/difference-between-threads-context-class-loader-and-normal-classloader and not use `getContextClassLoader()`. – Progman May 23 '21 at 19:53
  • Don’t convert a resource to a file path. Such code will not work when packaged as a .jar file. Instead, read the resource using [getResourceAsStream](https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/lang/Class.html#getResourceAsStream(java.lang.String)). Also, URL.getPath() does not return a valid file path for paths which have invalid URL characters in them, like spaces. – VGR May 23 '21 at 21:28
  • As your code stands, it's effectively untestable. `static` is the cause of all evil in testing, and you really /should/ be using spring too. – Software Engineer May 23 '21 at 23:28
  • @SoftwareEngineer I am avoid spring as I am writing my code for AWS lambda, so keeping it lightweight. What do you mean my untestable with static. How do you test your static utility classes? – User45 May 24 '21 at 18:16

0 Answers0