[英]Change property file located in src/test/resources
I'm exploring Apache Common Configuration and want to make a test that just read/write properties from/to xml file located in src/test/resources from the test. 我正在探索Apache通用配置,并且想要进行一个测试,该测试仅从测试中的src / test / resources中的xml文件中读取/写入属性。 So far, I'm reading with no problems but cannot write anything to this file.
到目前为止,我的阅读没有问题,但是无法向该文件写入任何内容。 If I change the location of the file from scr/test/resources to another location in the file system (example: c:/test), everything works fine.
如果我将文件的位置从scr / test / resources更改为文件系统中的另一个位置(例如c:/ test),则一切正常。 Can you please help me with this ?
你能帮我吗?
EDIT 编辑
Here is what I had tried so far: 这是我到目前为止尝试过的:
@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();
}
Thanks. 谢谢。
Well, you don't provide much insight about the running context, but from the src/test/resources path, I guess you work with Maven. 好吧,您没有提供有关运行上下文的太多见解,但是从src / test / resources路径来看,我想您使用的是Maven。 If this is true, this is what happens:
如果是这样,则会发生以下情况:
If you check this file, it works. 如果您检查此文件,它将起作用。 I just looked at the source file, not the file actually read from your code.
我只是查看了源文件,而不是实际从您的代码中读取的文件。
The difference with the second test, c:/temp/data.xml , is that the former is a relative path, while the latter is absolute, and not under Maven's scope. 与第二个测试c:/temp/data.xml的区别在于,前一个是相对路径,而后者是绝对路径,不在Maven的范围内。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.