更改位于 src/test/resources 的 属性 文件

Change property file located in src/test/resources

我正在探索 Apache 通用配置,并希望进行测试,仅 read/write 属性 from/to xml 文件位于测试的 src/test/resources 中。 到目前为止,我阅读没有任何问题,但无法向该文件写入任何内容。 如果我将文件的位置从 scr/test/resources 更改为文件系统中的另一个位置(例如:c:/test),一切正常。 你能帮我解决这个问题吗?

编辑

这是我到目前为止尝试过的方法:

    @Test
public void test() throws ConfigurationException {
    //First with data.xml located in src/test/resources
    XMLConfiguration config = new XMLConfiguration("data.xml");

    List<Object> rows = config.getList("user.username");
    System.out.println(rows);

    //This is not working
    config.setProperty("user.username", "FromTest");
    config.save();      

    // Second with data.xml in different location
    XMLConfiguration config = new XMLConfiguration("c:/temp/data.xml");

    List<Object> rows = config.getList("user.username");
    System.out.println(rows);   

    //This works
    config.setProperty("user.username", "FromTest");
    config.save();
}

谢谢。

好吧,您没有提供关于 运行 上下文的太多见解,但从 src/test/resources 路径来看,我猜您使用的是 Maven。 如果这是真的,则会发生以下情况:

  1. Maven复制资源到target/test-classes
  2. 测试方法对复制的资源进行操作,因此target/test-classes/data.xml

如果你检查这个文件,它是有效的。我只是查看了源文件,而不是实际从您的代码中读取的文件。

与第二个测试c:/temp/data.xml的区别在于前者是相对路径,而后者是绝对路径,并且不在 Maven 的范围内。