簡體   English   中英

使用Java所有者aeonbits進行測試

[英]Testing with java owner aeonbits

我一直在使用Java OWNER進行基於屬性的配置。

我創建了一個靜態方法

public static final ApplicationConfiguration config = ConfigFactory.create(ApplicationConfiguration.class,
        System.getProperties(), System.getenv());

然后將類導入到需要配置的任何地方。

不用說,單元測試是PITA。 我找不到覆蓋配置中值的好方法。

我想避免在每個類中將配置作為依賴項傳遞。 它增加了很多細節,從設計的角度來看,這是沒有意義的。 同樣適用於在每個類中調用配置工廠

ApplicationConfiguration config = ConfigFactory.create(ApplicationConfiguration.class,
        System.getProperties(), System.getenv());

你有什么建議嗎? 有最佳實踐嗎?

兩件事情:

  1. 您可以創建一個提供屬性的類,所有用戶都可以使用它:

     public class PropertiesAccessor { private static MyConfiguration mMyConfig = ConfigFactory.create(MyConfiguration.class); private PropertiesAccessor() // No need to allow instantiation of this class } /** * Get properties for this application * * @return Properties */ public static MyConfiguration getProperties() { return mMyConfig; } // for unit testing @VisibleForTesting public static void setProperties(MyConfiguration config) { mMyConfig = config; } } 

    現在,在任何需要屬性的地方,都可以使用此靜態方法

     PropertiesAccessor.getProperties() 
  2. 注意,有一個測試方法setProperties()。 有多種使用此方法的方法。 您可以創建一個測試屬性文件,將其加載,然后調用setProperties()方法。 我喜歡這樣的實用方法:

     public static void initProperties(String fileName) { Properties testProperties = new Properties(); File file = new File(fileName); FileInputStream stream = null; try { stream = new FileInputStream(file); testProperties.load(stream); } catch (IOException e) { // Log or whatever you want to do } finally { if (stream != null) { try { stream.close(); } catch (IOException e) { // Log or whatever you want to do; } MyConfiguration config = ConfigFactory.create(MyConfiguration.class, testProperties); PropertiesAccessor.setProperties(config); } 

    然后,您可以擁有各種屬性文件。

    或者,如果您只想設置一些屬性,請執行以下操作:

      Properties testProperties = new Properties(); testProperties.setProperty("key1", "data1"); testProperties.setProperty("key2", "data2"); final MyConfiguration myConfig = ConfigFactory.create(MyConfiguration.class, testProperties); PropertiesAccessor.setProperties(myConfig); 

不用說,單元測試是PITA。 我找不到覆蓋配置中值的好方法。

您可以具有可變配置對象。 閱讀手冊時沒有PITA。

但是有更好的方法。

當您了解SOLID原理時,界面非常易於測試和處理。

import static org.mockito.Mockito.*;

MyConfig cfg = mock(MyConfig.class); // mock object
when(cfg.myConfigurationThing()).thenReturn("whateverYourObjectNeeds");

ObjectThatYouNeedToTest targetObject = 
    new ObjectThatYouNeedToTest(cfg); // Dependency Injection 
                                   // see: http://wiki.c2.com/?ConstructorInjection

assertEquals("expected result", targetObject.whateverYouNeedToTest());

// then you can also verify interactions:

verify(cfg, times(1)).myConfigurationThing();

上面的代碼片段顯示了一個Mockito示例,但是有很多類似的測試框架。

檢查什么是模擬對象 ,什么是依賴注入接口隔離

我想避免在每個類中將配置作為依賴項傳遞。

如果對象需要配置,則應將配置傳遞給對象。 這就是應該的方式。 如果太冗長,則您的應用程序設計中需要進行一些修改。 例如,您選擇在類中具有靜態成員的單例,這不是很好 ,特別是對於測試。

同樣適用於在每個類中調用配置工廠

在每個類中都調用config工廠,這是一個壞主意。 也許您可以將配置文件拆分為許多特定於組件的文件。 那是一種方式(我的方式)。

但是,如果您只需要一個巨大的配置文件(這不是我最喜歡的方法),則您的配置接口不需要遵循相同的結構:您可以從同一個文件中讀取多個組件配置接口:您使用熱重載,我可能可以做更多的事情來使嵌套的配置界面樹中的配置對象模塊化。
但是,嘿,我在業余時間為自己做到了,我免費分享了它; 如果人們做的事情與我不同,並且他們需要我來支持他們的工作方式,那么也許他們可以支持開發貢獻好的代碼或花我的時間來改進它。

對不起,PITA。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM