我有一个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