【问题标题】:How to override your Propertyfile for jUnit (maven)如何覆盖 jUnit 的 Propertyfile(maven)
【发布时间】:2013-05-04 16:11:18
【问题描述】:

我有一个属性文件config.properties,其中存储了一个类加载以读取文件的路径。 属性加载如下:

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);
}

}

而相关类中的调用是这样的:

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

出于测试目的,我希望能够更改测试目录的路径,或更改 jUnit-TestCase 中的整个属性文件。如何做到这一点?

如果有帮助,我正在使用 Maven。

【问题讨论】:

  • 如果您将config.properties 放置在src/main/resources,它将能够将新的放置在src/test/resources

标签: maven properties overriding


【解决方案1】:

假设你有你的 config.properties

src/main/resources/config.properties

注意:您仍然应该将属性文件放在 src/main/resources

的某个位置

将您的测试配置放入

src/main/test/config.properties

就是这样。无需更改您的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-23
    • 1970-01-01
    • 2018-05-16
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    相关资源
    最近更新 更多