Java 无法从属性文件中获取属性?

Java 无法从属性文件中获取属性?,java,Java,我已经编写了一个类FetchProp,它扩展了从属性文件中获取值的类PropMap。当我作为junit测试用例运行FetchProp时,它给了我正确的输出,即它以我想要的方式获取属性。然后我创建了一个类CreateReport,它调用FetchProp的方法来帮助生成报告。但是当我运行CreateReport上的测试用例FetchProp没有像独立运行时那样获取属性 有人能提出可能的问题吗 PS:我已经检查了属性文件所在的路径,每次运行程序时都会显示属性文件路径,并且该文件具有我想要获取的属性。

我已经编写了一个类FetchProp,它扩展了从属性文件中获取值的类PropMap。当我作为junit测试用例运行FetchProp时,它给了我正确的输出,即它以我想要的方式获取属性。然后我创建了一个类CreateReport,它调用FetchProp的方法来帮助生成报告。但是当我运行CreateReport上的测试用例FetchProp没有像独立运行时那样获取属性

有人能提出可能的问题吗


PS:我已经检查了属性文件所在的路径,每次运行程序时都会显示属性文件路径,并且该文件具有我想要获取的属性。

这可能取决于您如何获取属性文件。如果您正在使用以下内容:

private Properties readProps() throws FileNotFoundException, IOException {
    File f = new File("c:\\path\\to\\file\\props.properties");
    Properties props = new Properties();
    props.load(new FileInputStream(f));
    return props;
}
private Properties readProps2() throws IOException  {
    InputStream stream = this.getClass().getResourceAsStream("props.properties");
    Properties props = new Properties();
    props.load(stream);
    return props;
}
…那么它可能在单元测试中工作,但在运行应用程序时不起作用。您可能想尝试以下方法:

private Properties readProps() throws FileNotFoundException, IOException {
    File f = new File("c:\\path\\to\\file\\props.properties");
    Properties props = new Properties();
    props.load(new FileInputStream(f));
    return props;
}
private Properties readProps2() throws IOException  {
    InputStream stream = this.getClass().getResourceAsStream("props.properties");
    Properties props = new Properties();
    props.load(stream);
    return props;
}

…它将从类路径读取文件。

您能否发布一些示例代码来显示所描述的行为?