user2368505
user2368505

Reputation: 426

How to override your Propertyfile for jUnit (maven)

I have a Propertyfile config.properties in which I store a Path which a class loads to read Files. The properties are loaded like this:

public class PropertyConfig {
private static final Properties properties = new Properties();

static {
    try {
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"));
    } catch (IOException e) {
        throw new ExceptionInInitializerError(e);
    }
}

public static String getSetting(String key) {
    return properties.getProperty(key);
}

}

and the call in the relevant class is like this:

private static File savedGamesFolder = new File(PropertyConfig.getSetting("folder_for_saved_games"));

For testing purposes I want to be able to change the path to a test directory, or change the whole Property-file in a jUnit-TestCase. How can achieve this?

I'm using Maven if that helps.

Upvotes: 1

Views: 1311

Answers (1)

rzymek
rzymek

Reputation: 9281

Assuming you have your config.properties in

src/main/resources/config.properties

Note: you should nevertheless have your properties files somewhere in src/main/resources

Place your test configuration in

src/main/test/config.properties

That's it. No need to change your code.

Upvotes: 1

Related Questions