【发布时间】:2016-04-15 06:02:01
【问题描述】:
我有一个 maven java 项目,在 src/main/resources 目录中有一个属性文件。我已经打包了jar,jar中没有属性文件,所以可以部署到不同设置的不同环境,但是单元测试失败了
项目结构是
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"));
标签: java maven junit properties