user2883938
user2883938

Reputation: 37

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. 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. 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.

Upvotes: 0

Views: 1130

Answers (1)

Dimitri Hautot
Dimitri Hautot

Reputation: 458

Well, you don't provide much insight about the running context, but from the src/test/resources path, I guess you work with Maven. If this is true, this is what happens:

  1. Maven copies resources to target/test-classes
  2. the test method operates on the copied resources, thus 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.

Upvotes: 1

Related Questions