gpt4 book ai didi

java - 坚持测试一种方法

转载 作者:行者123 更新时间:2023-11-29 08:18:49 25 4
gpt4 key购买 nike

我正在用 word 进行一些表测试,所有的 JUnit 都已完成,但我在测试方法时遇到了问题 - 因为我是这个项目的测试人员而不是编码人员,所以我很难理解什么是正确的,什么是错误的

   public GregorianCalendar calcDeparture(String date, String time) {
String[] calDate = new String[3];
String[] calTime = new String[2];
calDate[0] = (date.substring(0, 2)); //Dat
calDate[1] = date.substring(2, 5); //Month
calDate[2] = "20" + date.substring(5, 7); //Year
calTime = time.split(":");

//Adds the year, month and day and hour and minute from the above splited arrays
int year = Integer.parseInt(calDate[2]);
int month = monthToInt(calDate[1]);
int day = Integer.parseInt(calDate[0]);
int hour = Integer.parseInt(calTime[0]);
int minute = Integer.parseInt(calTime[1]);

GregorianCalendar newDeparture = new GregorianCalendar(year, month, day, hour, minute, 0);
return newDeparture;
}

这是我正在测试的方法。如果我将“01AUG07“14:40”的值传递给它,我会得到一个公历日历,但我不知道它里面的值是否正确,所以我无法勾选通过或失败的框。我得到了什么在 BlueJ 对象检查器中是一大堆很长的数字 :D

我能得到一些帮助吗

谢谢

最佳答案

我建议使用 SimpleDateFormat() 同时检查日历的所有相关值,如下所示:

SimpleDateFormat f = new SimpleDateFormat ("yyyy-MM-dd HH:mm");
String s = f.format (calcDeparture(yourDate, yourTime));
assertEquals ("2007-08-01 14:40", s);

现在用奇数日期(例如 2999 年 12 月 31 日、2001 年 8 月 45 日、2001 年 2 月 29 日等)调用您的方法,看看会得到什么以及应该如何处理错误。

关于java - 坚持测试一种方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2041911/

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