我在此文件夹中当前有一个属性文件:
/src/webapp/WEB-INF/classes/my.properties
我正在使用它加载:
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream is = classLoader.getResourceAsStream("/my.properties");
Properties props = new Properties();
try {
props.load(is);
} catch (IOException e) {
e.printStackTrace();
}
String test = props.getProperty("test");
现在,这在我的Spring mvc应用程序中可以正常工作。
但是,当我为此创建一个测试时,它失败了,我认为是因为应用程序加载它的方式未使用web-inf / classs,因为它只是一个类,而不是spring Web应用程序。
那么我应该将属性文件放在哪里,以便在运行junit测试时可以拾取属性文件?
另外,对于我的Web应用程序,默认类路径中除/ web-inf / classes之外还有哪些其他文件夹?
最佳答案
如果将my.properties
放在maven中的/src/test/resources
下,它将作为常规资源用于您的测试。