JUnit测试List内的map的内容

时间:2013-06-11 07:32:06

标签: java android map junit arraylist

我想测试列表中的地图内容(列表>) 在我的Testclass中,我在List中填充了一个Details of Map。一切都很好,直到这里,我现在的问题是,我如何检查地图的内容而不只是检查地图的参考?

以下是测试方法:

    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;     
}

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

    @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为String或只是布尔值。

任何有一点建议或小费的人,我怎么能做得更好?

由于

1 个答案:

答案 0 :(得分:1)

我认为这一行:

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

应该是

assertEquals("test", test.get("Terminname"));