2013-05-10 19 views
1

我有一個Propertyfile config.properties其中我存儲了一個類加載讀取文件的路徑。 屬性加載這樣的:如何覆蓋您的Propertyfile的jUnit(maven)

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中更改整個Property文件。如何實現這一目標?

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

+1

如果將'config.properties'放在'src/main/resources'中,它就可以將新的'src/test/resources'放入。 – 2013-05-10 03:30:09

回答

1

假設你有你的config.properties在

src/main/resources/config.properties 

注:你仍然應該有你的屬性文件中的某個地方src/main/resources

將您的測試配置中

src/main/test/config.properties 

這它。無需更改您的代碼。