我有一個Maven Java項目,在src/main/resources目錄中有一個屬性文件。我已經打包的jar無屬性的JAR文件,因此它可以被部署到不同的環境使用不同的設置,但單元測試失敗如何從Maven Java項目加載外部屬性文件
這個項目的結構
Properties Application Project
|-- pom.xml
`-- src
|-- main
|-- java
| `-- java classes
|-- resources
| `-- myProps.properties
`-- target
|--properties-app.jar
myProps.properties
的值在這個文件中加載
public class MyProperties {
private Properties myProperties;
public MyProperties() {
File file = new File("./myProps.properties");
try {
myProperties = new Properties();
FileInputStream myPropertiesInputStream = new FileInputStream(file);
myProperties.load(myPropertiesInputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public Properties getPropertyValue() {
return myProperties.getProperty("value");
}
}
單元測試如下
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class MyPropertiesTest {
private MyProperties myProperties;
@Before
public void setup() {
myProperties = new MyProperties();
}
@Test
public void testMyProperties() {
assertNotNull(myProperties.getPropertyValue());
}
}
我應該使用什麼來從目標目錄加載屬性文件,以便我可以從命令行成功構建它?
'myProperties.load(的getClass()的getResourceAsStream( 「/ myProps.properties」));' – MadProgrammer