0

我想测试列表(列表>)中地图的内容在我的测试类中,我用一个详细信息地图填充了列表。直到这里一切都很好,我现在的问题是,如何检查地图的内容而不仅仅是检查地图的引用?

这是用于测试的方法:

    private Date startDate;
private Date endDate;

    public static List<Map<String, String>> convertListToMap(List<Appointment> appointmentList) throws java.text.ParseException {

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

    SimpleDateFormat germanTimeFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm");

    if (appointmentList != null && !appointmentList.isEmpty()) {

        HashMap<String, String> map; 

        for (Appointment appointment : appointmentList) {

            map = new HashMap<String, String>(); 

            map.put("id",  String.valueOf(appointment));
            map.put("Terminname", appointment.getName());
            map.put("Datum u. Uhrzeit", germanTimeFormat.format(appointment.getStartDate())); 
            map.put("Enduhrzeit", germanTimeFormat.format(appointment.getEndDate()));

            appointmentsListWithMap.add(map);
        }   
    }
    return appointmentsListWithMap;     
}

这就是我试图测试它的方式:

    @Test
public static void testConvertListToMap() throws java.text.ParseException {

    Appointment tester = new Appointment();
    ArrayList<Appointment> appList = new ArrayList<Appointment>();
    List<Map<String, String>> appointmentsListWithMap = new ArrayList<Map<String, String>>();

    try {           
        JSONArray appointmentsJSONArray;

    String startDate = "2013-05-21 13:00:00";
    String endDate = "2013-05-21 14:00:00";

        Calendar cal = GregorianCalendar.getInstance(); // creates calendar
        cal.setTimeZone(TimeZone.getTimeZone("CET"));

        SimpleDateFormat readFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");                         
        Date startingDate = readFormat.parse(startDate);
        Date endingDate = readFormat.parse(endDate);


        Appointment app = new Appointment();

        app.setName("test");
        app.setStartDate(startingDate);
        app.setEndDate(endingDate);
        app.setTreshold(1);

        appList.add(app);


        List<Map<String, String>> appMap = Appointment.convertListToMap(appList);
        assertNotNull(appMap);
        Map<String, String> test = appMap.get(0);
        assertNotNull(test);
        assertEquals("Terminname=test", appMap.getName(0));

        if (test.isEmpty()) {
            fail("empty");
        }

    } catch (java.text.ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
}

我尝试了几种方法来测试 Mapcontent,但我仍然不明白。结果就像比较引用,整个 Map 作为字符串或只是布尔值。

任何有一点建议或提示的人,我怎样才能做得更好?

谢谢

4

1 回答 1

1

我认为这条线:

assertEquals("Terminname=test", appMap.getName(0));

应该

assertEquals("test", test.get("Terminname"));
于 2013-06-11T07:39:33.320 回答