2017-07-03 129 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 
    }