简体   繁体   English

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

[英]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: 如果是这样,则会发生以下情况:

  1. Maven copies resources to target/test-classes Maven将资源复制到目标/测试类
  2. the test method operates on the copied resources, thus target/test-classes/data.xml 测试方法对复制的资源进行操作,因此为target / test-classes / data.xml

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.

相关问题 位于“测试资源根”中时找不到文件 - File not found when located in “Test Resources Root” 访问位于src文件夹之外的jar文件中的资源 - Accessing resources in a jar file that are located outside the src folder 无法访问src / main / resources /下的文件 - Can't get to a file located under src/main/resources/ 在路径“” src / test / resources /”上找不到文件 - file not found on path “”src/test/resources/" 每次更改src / main / resources下的属性文件时,都必须重新构建Jar - I have to rebuild the Jar everytime I change the property file under src/main/resources 从src / test / resources进行单元测试中的NIO加载文件 - NIO load file in an unit test from src/test/resources Spring JUnit Test 加载 src/test/resources 之外文件的属性 - Spring JUnit Test load properties of file outside of src/test/resources Spring xml + JUnit-xml上下文文件在src / test / resources中不起作用,但是在src / main / resources中起作用 - Spring xml + JUnit - xml context file not working in src/test/resources but works in src/main/resources spring - @ContextConfiguration无法在src / test / resources中加载配置文件 - spring - @ContextConfiguration fail to load config file in src/test/resources 使用getResourceAsStream()从src / test / resources获取文件 - get file from src/test/resources using getResourceAsStream()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM