为json数据编写junit测试用例

时间:2016-12-08 14:11:34

标签: java json junit hashmap

我的控制器类中有一个方法

    @RequestMapping(value = "/getSearchData/{fdate}/{tdate}", method = RequestMethod.GET)
    public @ResponseBody List<Map<String, Object>> getSearchData(@PathVariable String fdate,
        @PathVariable String tdate) {
    System.out.println("fdate============" + fdate);
    System.out.println("tdate============" + tdate);
    String frdate = fdate.substring(2, 4) + "/" + fdate.substring(0, 2) + "/" + fdate.substring(4, 8);

    String todate = tdate.substring(2, 4) + "/" + tdate.substring(0, 2) + "/" + tdate.substring(4, 8);
    String json = null;
    List<Object[]> list = retrievePublicationService.getsearchData(frdate, todate);

    List<Map<String, Object>> activeTeamMap = new ArrayList<Map<String, Object>>();

    if (list != null) {

        for (Object[] obj : list) {
            Map<String, Object> dropDownData = new HashMap<String, Object>();
            dropDownData.put("publicationId", obj[0]);
            dropDownData.put("documentId", obj[1]);
            dropDownData.put("documentType", obj[2]);
            dropDownData.put("languageCode", obj[3]);
            dropDownData.put("processStartDate", obj[4]);
            dropDownData.put("publicationType", obj[5]);
            activeTeamMap.add(dropDownData);
            System.out.println("activeTeamMap==vbnbvn==" + activeTeamMap);
        }

    } else {

    }

    return activeTeamMap;
}

我需要为这个方法编写一个junit测试用例。我是junit的新手所以我不知道如何继续这个。任何形式的帮助将不胜感激。 :(

1 个答案:

答案 0 :(得分:1)

嗯,你想测试你的方法的作用。它有什么作用?

我明白了,它从一些字符串开始,从日期和日期开始。所以你想测试一下。你是怎样做的?我看,它调用了retrievePublicationService。因此,您可以在retrievePublicationService上创建一个观察者(使用Mockito),并验证它是否使用您的子网格参数调用它。伪代码:

RetrievePublicationService retrievePublicationService = Mockito.mock(RPS.class)
String fromDate = "real from date here";
String toDate = "real to date here";
TheClassUnderTest tcut = new TheClassUnderTest(retrievePublicationService);
String substringedFromDate = "expected from date";
String substringedToDate = "expected to date";

tcut.getSearchData(fromDate, toDate);

Mockito.verify(retriebePublicationService).getSearchData(eq(substringedFromDate ), eq(substringedToDate ));

现在&#34;专业提示&#34;:日期的子字符串不属于此方法,方法应该只做一件事。

我还能看到什么......这个方法的作用主要是将Object []数组从发布服务映射到Map。所以我们应该测试,映射是有效的。伪代码

...
String expectedPublicationId = "expectedPublicationId";
List<Object[]> searchDataPubService = new ArrayList<>();
searchData.add(new Object[]{expectedPublicationId, and the other expectedThingies});
Mockito.when(mockedRetrievePublicationService).getSearchData(any(String.class), any(String.class)).thenReturn(searchDataPubService ); <- we tell mockito here, it should return your list, for any call.

List<Map<String, Object>> searchData = classUnderTest.getSearchData(fromDate, toDate);

assertEquals(expectedPublicationId, searchData.get(0).get("publicationId")); ... since you map the [0]th entry from the object array to they key "publicationId" in the Map
... and the other assertions

映射本身也可以提取到一个单独的方法......所以最后你的方法应该是这样的:

List<Map<String, Object>> getSearchData(String fdate, String tdate){
   String fromDate = parseDate(fdate);
   String toDate = parseDate(tdate);

   List<Object[]> foundData = retrievePublicationService.getSearchData(fromDate, toDate);

   return mapSearchResultToDropdown(foundData);
}

你看我在那里做了什么?我已经提取了你的问题 - 解析和映射 - 所以这个方法更容易阅读。并且更容易测试,因为可以单独测试日期的映射和解析。

那么剩下要测试的是什么?如果您将parseDate提取为单独的类型,您还可以验证,使用fdate调用parseDate一次,使用tdate调用一次。然后,您可以验证是否已使用已解析的日期调用了retrievePublicationService。而且你必须将映射方法提取为单独的类型,以测试它是否已经使用数据调用,而retrievebePublicationService将返回...

嗯...抱歉长篇文章,希望它有所帮助:P