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.