2017-07-03 128 views
-1

如何在JUnit中調用populateMapWithFormattedDates方法以及如何爲此方法編寫JUnit populateMapWithFormattedDates。我不知道如何爲嵌套方法編寫JUnit,因此非常有幫助。如何爲此代碼編寫junit?

protected Map<String, String> populateDispatch(final RequestDispatchData requestDispatchData) 
    { 
     final Map<String, String> map = getDispatchFieldMapper().populateMapper(requestDispatchData); 
     populateMapWithFormattedDates(requestDispatchData, map); 
} 


private void populateMapWithFormattedDates(final RequestDispatchData requestDispatchData, final Map<String, String> map) 
    { 
     String dateFormatted = map.get("ticket_date"); 
     Date date = null; 
     try 
     { 
      date = new SimpleDateFormat("MM/dd/yy").parse(dateFormatted); 
     } 
     catch (ParseException parseException) 
     { 
      customLogger.logMessage(diagnosticMethodSignature, DiagnosticType.EXCEPTION, 
        "Exception in parsing start date of ticket " + parseException); 
     } 
     map.put("startDateDDMMYY", DateEnum.DDMMYY.getFormattor().format(date)); 
     map.put("startDateDDMMMYY", DateEnum.DDMMMYY.getFormattor().format(date)); 
     map.put("startDateDMY", DateEnum.DMY.getFormattor().format(date)); 
     map.put("startDateYYMMDD", DateEnum.YYMMDD.getFormattor().format(date)); 
    } 
+1

如果你知道什麼'private'關鍵字做,你必須知道你將無法在你的測試類中調用'populateMapWithFormattedDates'。你需要爲'populateDispatch'編寫測試,測試它所調用的所有私有方法。 – Mritunjay

+1

我更喜歡的理論是單元測試應該測試一個類的公開可見的行爲,這意味着你不需要爲私有方法編寫單元測試。 – ajb

回答

0

沒有什麼比如Java中的嵌套方法。這是一個嵌套的函數調用是什麼。另外,是的,你不能通過它的對象調用一個類的私有函數,所以通過調用它們來單獨測試它們是不可能的。

雖然您可以讓公共或受保護的函數有點像getter一樣進行調用。

0

我相信你的代碼是一些什麼樣的,

protected Map<String, String> populateDispatch(final RequestDispatchData requestDispatchData) 
    { 
     final Map<String, String> map = getDispatchFieldMapper().populateMapper(requestDispatchData); 
     return populateMapWithFormattedDates(requestDispatchData, map); 
} 

注意,你已經錯過了return語句,並更新某些條件地圖,

private void populateMapWithFormattedDates(final RequestDispatchData requestDispatchData, final Map<String, String> map) 
    { 
// Map manipulation here 
} 

所以,如果你有最低依賴於getDispatchFieldMapper()。populateMapper(),那麼你可以直接調用populateDispatch()從你的測試代碼,否則你可能需要找到一種方法來注入一個習慣m實現DispatchFieldMapper來準備測試你的目標方法的地圖。

DispatchFieldMapper的注入可以通過重寫getDispatchFieldMapper()或在您的課上使用setDispatchFieldMapper()

在準備您的自定義DispatchFieldMapper時,確保populateMapper()返回包含測試所需的所有數據的地圖。

0

在直接測試類的測試中調用非可訪問方法並不是一個好主意。第二件事:不可訪問的方法總是被稱爲形式一些可訪問的方法或範圍,否則代碼是死代碼只是刪除它。

因爲方法是privet,所以如果它正在使用,那麼它從當前類的代碼調用某處。在你的代碼中它的形式爲populateDispatch,所以populateMapWithFormattedDates方法編寫測試用例的實際方法是覆蓋populateDispatch方法的所有場景,而populateDispatch也用當前類的子類調用它的形式。

但你可以調用私有方法在JUnit這樣的:

Deencapsulation.invoke(<object of class in called method is exist>, "populateMapWithFormattedDates", <object of RequestDispatchData class>, <object of Map<String, String> class>); 

再次它是一種方法來調用私有方法,但你不應該使用這個......

1

簡單:你不考直接使用專用方法。而是專注於從外部調用的那些方法的「公共契約」。在你的情況,這將是:

Map<String, String> populateDispatch(... 

因此,你需要寫這樣的測試:

@Test 
public void populateDispatchForValidDate() { 
    RequestDispatchData request = ... 
    Map<String, String> actualOutput = underTest.populateDispatch(request); 
    assertThat(actualOutput.size(), is(5)); 
} 

以上只是意味着作爲一個例子。它做什麼:

  • 創建一個「請求」對象。這可能是一個模擬;或者一個真實的對象 - 取決於你使用這個對象的各種方法。它是多麼容易建立一個「真正的」 RequestDispatchData對象與它調用該測試方法
  • 斷言結果一個/多個屬性回來

展望「測試數據」

  • 在您的生產代碼中,代碼在單一方法中做了太多事情。您可能想閱讀「乾淨的代碼」並改進代碼。這可能會導致創建一些幫助類,這將更容易測試。

  • 0

    你應該去耦populateMapWithFormattedDates方法是這樣的:

    // I created an utility class but it's a suggestion. 
    // I'm using an util class because you don't use requestDispatchData for 
    // anything. But if you do, maybe it's a good idea to implement this code 
    // on RequestDispatchData class 
    class DispatchMapUtils { 
        // Note that I took of the requestDispatchData 
        public static Map<String, String> populateMapWithFormattedDates(final Map<String, String> map) throws ParseException { 
         // Your code without try-catch. 
         // Throw the exception to the caller of this method 
         // and try-catch there to use the customLogger 
        } 
    } 
    

    有了這個代碼,您的測試將是這樣的:

    @Test 
    public void shouldFormatTicketDateInVariousFormat() { 
        Map<String, String> map; 
        // Instantiate and put some initial datas 
        map = new ... 
        map.put('ticket_date') = .. 
        // Call the method! 
        DispatchMapUtils.populateMapWithFormattedDates(map); 
        // Do the assertions! 
        Assert.assertTrue(map.get("startDateDDMMYY").equals(...)); 
    } 
    
    @Test 
    public void shouldThrowExceptionWhenTicketDateIsInvalid() { 
        // More testing code 
    }