*Please forgive the intricate title*
Background
/pom.xml
...
<foo.bar>stackoverflow</foo.bar>
...
/src/main/resources/config.properties
...
foo.bar=${foo.bar}
...
Config.java
...
public final static String FOO_BAR;
static {
try {
InputStream stream = Config.class.getResourceAsStream("/config.properties");
Properties properties = new Properties();
properties.load(stream);
FOO_BAR = properties.getProperty("foo.bar");
} catch (IOException e) {
e.printStackTrace();
}
}
...
Question
In /src/main/java, I’m using Config.FOO_BAR in MyClass.java. If I want to test MyClass in a /src/test/java folder using JUnit with MyClassTest.java, how can I load the properties so that the Config.FOO_BAR constant get initialized?
I tried to add a hardly-written config.properties within /src/test/resources with foo.bar=stackoverflow, but it still can’t get initialized.
I could make it work by changing some in your
pom.xmland yourConfig.java.Add these lines to your
pom.xml:And change the order of some lines in
Config.java:Output if running
Config:Disclaimer
I am not sure what purpose you have with setting these static config values. I just made it work.
Edit after comment
Added a simple JUnit test to
src/test/java/:No problems with this test.