gpt4 book ai didi

java - JUnit 测试列表内映射的内容

转载 作者:行者123 更新时间:2023-12-01 14:27:55 24 4
gpt4 key购买 nike

我想测试列表(List>)内 map 的内容在我的测试类中,我用一张详细信息图填充了列表。到这里一切都很好,我现在的问题是,如何检查 map 的内容而不仅仅是检查 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;
}

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

    @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();
}
}

我尝试了多种方法来测试 map 内容,但我仍然不明白。结果就像比较引用一样,整个 Map 作为字符串或只是 boolean 值。

有人能给我一些建议或提示吗,我怎样才能做得更好?

谢谢

最佳答案

我认为这一行:

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

应该是

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

关于java - JUnit 测试列表内映射的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17038742/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com