如何为这段代码编写junit?

时间:2017-07-03 04:07:45

标签: java junit

如何在JUnit中调用populateMapWithFormattedDates方法以及如何为此方法编写JUnit populateMapWithFormattedDates。我不知道如何为嵌套方法编写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));
    }

5 个答案:

答案 0 :(得分:1)

简单:您不能直接测试私有方法。

相反,你专注于&#34;公共合同&#34;那些从外部调用的方法&#34;&#34;在你的情况下,那将是:

Map<String, String> populateDispatch(...

因此,您需要编写类似的测试:

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

以上仅作为一个例子。它的作用:

  • 创建&#34;请求&#34;宾语。这可能是一个模拟;或者一个真实的对象 - 取决于你的各种方法对这个对象的确切做法。创造一个真正的&#34;是多么容易RequestDispatchData对象带有&#34;测试数据&#34;
  • 它调用测试中的方法
  • 断言结果的一个/多个属性

查看您的生产代码,该代码在该单个方法中执行的过程太多。您可能想要阅读&#34;清洁代码&#34;并改进该代码。这可能会导致创建一些更容易测试的辅助类。

答案 1 :(得分:0)

Java中没有嵌套方法。这是一个嵌套的函数调用就是它。另外,是的,你不能通过它的对象调用类的私有函数,因此不可能通过调用它们来单独测试它们。

你可以使用公共或受保护的函数来执行调用,就像getter一样。

答案 2 :(得分: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(),否则您可能拥有找到一种方法来注入DispatchFieldMapper的自定义实现,以准备测试目标方法的地图。

注入DispatchFieldMapper可以通过覆盖 getDispatchFieldMapper()或在您的类上使用 setDispatchFieldMapper()

在准备自定义DispatchFieldMapper时,请确保 populateMapper()返回包含测试所需数据的地图。

答案 3 :(得分:0)

  

在直接测试测试类时,调用不可访问的方法并不是一个好主意   第二件事:不可访问的方法总是被称为一些可访问的方法或范围,否则代码是死代码只是删除它。

因为方法是privet,所以如果它正在使用,那么它从当前类的代码调用某处。在您的代码中,它调用了表单populateDispatch,因此为populateMapWithFormattedDates方法编写测试用例的实际方法涵盖了populateDispatch方法的所有方案,populateDispatch也用于子类当前的类在那里调用它。

但你可以在junit中调用private方法,如:

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

同样,这是一种调用私有方法的方法,但你不应该使用它......

答案 4 :(得分: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
}