Java 我应该把测试的属性文件放在哪里?

Java 我应该把测试的属性文件放在哪里?,java,web-applications,junit,classpath,maven,Java,Web Applications,Junit,Classpath,Maven,我在这个文件夹中有一个属性文件: /src/webapp/WEB-INF/classes/my.properties 我正在使用以下方式加载它: ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); InputStream is = classLoader.getResourceAsStream("/my.properties"); Properties props = new Properties

我在这个文件夹中有一个属性文件:

/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");
现在,这在我的SpringMVC应用程序中运行良好

但是当我为此创建一个测试时,它失败了,我假设应用程序加载它的方式不是使用web inf/类,因为它只是一个类而不是spring web应用程序

那么,我应该将属性文件放在哪里,以便在junit测试运行时提取属性文件?


另外,对于我的web应用程序,除了/web inf/classes之外,默认类路径中还有哪些文件夹?

通常我会将属性文件直接放在src文件夹中。

如果您将
my.properties
放在
/src/test/resources
下,它将作为测试的正常资源使用。

我会删除路径(/)在
classLoader.getResourceAsStream(“/my.properties”)
String filename = "my.properties"; 
InputStream is = classLoader.getResourceAsStream(filename);  //for web-app
if(is == null)
    is = new FileInputStream (filename);        //for testing

我相信您的路径是:
/src/webapp/WEB-INF/classes/my.properties
,但它怎么会与WEB应用中的/一起工作呢?困惑?类加载器已经在根目录中搜索,所以/没有任何区别。另一方面,如果您尝试
class.getResourceAsStream(filename)
则会在与该类相同的包(文件夹)中搜索资源,因此在这种情况下,如果该文件位于根目录中,则需要/。