Java 如何为此代码编写junit?

Java 如何为此代码编写junit?,java,junit,Java,Junit,如何在JUnit中调用populateMapWithFormattedDates方法,以及如何为此方法编写JUnit populateMapWithFormattedDates。我不知道如何为嵌套方法编写JUnit,所以请帮忙 protected Map<String, String> populateDispatch(final RequestDispatchData requestDispatchData) { final Map<String, S

如何在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));
    }
受保护映射填充的Dispatch(最终请求DispatchData请求DispatchData)
{
最终映射图=getDispatchFieldMapper().populateMapper(requestDispatchData);
使用格式化日期(requestDispatchData、map)填充map;
}
private void populateMapWithFormattedDates(最终请求DispatchData请求DispatchData,最终映射)
{
String dateFormatted=map.get(“票证日期”);
日期=空;
尝试
{
日期=新的简化格式(“MM/dd/yy”)。解析(日期格式);
}
捕获(解析异常解析异常)
{
日志消息(diagnosticMethodSignature、DiagnosticType.EXCEPTION、,
“票证解析开始日期异常”+解析异常);
}
put(“startDateDDMMYY”,DateEnum.DDMMYY.getFormattor().format(date));
put(“startDateDDMMMYY”,DateEnum.DDMMMYY.getFormattor().format(date));
put(“startDateDMY”,DateEnum.DMY.getFormattor().format(date));
put(“startdateyymdd”,DateEnum.yymdd.getFormattor().format(date));
}

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


您可以使用一个公共或受保护的函数来执行类似getter的调用。

我相信您的代码是这样的

protected Map<String, String> populateDispatch(final RequestDispatchData requestDispatchData)
    {
        final Map<String, String> map = getDispatchFieldMapper().populateMapper(requestDispatchData);
        return populateMapWithFormattedDates(requestDispatchData, map);
 }
受保护映射填充的Dispatch(最终请求DispatchData请求DispatchData)
{
最终映射图=getDispatchFieldMapper().populateMapper(requestDispatchData);
返回populateMapWithFormattedDates(requestDispatchData、map);
}
请注意,您错过了return语句,并在特定条件下从

private void populateMapWithFormattedDates(final RequestDispatchData requestDispatchData, final Map<String, String> map)
    {
// Map manipulation here
}
private void populateMapWithFormattedDates(最终请求DispatchData请求DispatchData,最终映射)
{
//这里的地图操作
}
因此,如果您对getDispatchFieldMapper().populateMapper()的依赖性最小,那么您可以直接从测试代码中调用populateDispatch(),否则您可能必须找到注入DispatchFieldMapper的自定义实现的方法,以便为测试目标方法准备映射

DispatchFieldMapper的注入可以通过重写getDispatchFieldMapper()或在类上使用setDispatchFieldMapper()来实现

在准备自定义DispatchFieldMapper时,请确保populateMapper()返回一个包含测试所需所有数据的映射

直接从测试类进行测试时,调用不可访问的方法不是一个好主意。
第二件事:不可访问的方法总是从某个可访问的方法或范围中调用,否则代码就是死代码,请删除它

因为方法是privet,所以若它正在使用,那个么它将从当前类的代码中调用。在您的代码中,它称为form
PopulatedDispatch
,所以为
populateMapWithFormattedDates
方法编写测试用例的实际方法是覆盖
PopulatedDispatch
方法的所有场景,
PopulatedDispatch
也用于当前类的子类,在那里调用它

但您可以在junit中调用私有方法,如:

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

同样,这是一种调用私有方法的方法,但您不应该使用这个…

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

相反,您关注的是那些“从外部”调用的方法的“公共契约”。在您的情况下,这将是:

Map<String, String> populateDispatch(...
Map PopulatedDispatch(。。。
因此,您需要编写如下测试:

@Test
public void populateDispatchForValidDate() {
  RequestDispatchData request = ...
  Map<String, String> actualOutput = underTest.populateDispatch(request);
  assertThat(actualOutput.size(), is(5));
}
@测试
公共无效已填充DispatchForValidDate(){
RequestDispatchData请求=。。。
映射actualOutput=未测试。填充的分发(请求);
断言(actualOutput.size(),为(5));
}
以上只是一个例子,它的作用是:

  • 创建一个“请求”对象。这可能是一个模拟对象,也可能是一个真实对象-这取决于您的各种方法对该对象的具体操作。以及使用“测试数据”创建一个“真实”RequestDispatchData对象的难易程度
  • 它调用被测试的方法
  • 它断言返回结果的一个/多个属性

查看您的生产代码,该代码在单个方法中做了太多的事情。您可能希望阅读“干净的代码”并改进该代码。这可能会导致创建一些帮助器类,这些类将更易于测试。

您应该像这样解耦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
}
//我创建了一个实用程序类,但这只是一个建议。
//我使用一个util类,因为您不使用requestDispatchData
//任何东西。但如果你这样做,也许实现这段代码是个好主意
//关于RequestDispatchData类
类DispatchMapUtils{
//注意,我使用了requestDispatchData
公共静态映射populateMapWithFormattedDates(最终映射)引发异常{
//您的代码没有try-catch。
//将异常抛出到此方法的调用方
//然后尝试在那里使用customLogger
}
}
使用此代码,您的测试将如下所示:

// 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
}
@测试
public void shouldFormatTicketDateInVariousFormat(){
地图;
//实例化并放置一些初始数据
地图=新的。。。
map.put('ticket_date')=。。
//调用方法!
DispatchMapUtils.populateMapWithFormattedDates(map);
//执行断言!
Assert.assertT