我正在学习Java Test-Driven Development,使用JUnit 4。我已经得到了一个测试场景,并且必须编写实现代码,才能使测试通过。
这是测试场景:
package java;
import static org.junit.jupiter.api.Assertions.assertFalse;
import java.time.LocalDateTime;
import org.junit.jupiter.api.Test;
class CallCenterTests {
private final CallCenter callCenter = new CallCenter();
private final LocalDateTime currentTime = LocalDateTime.of(2021, 1, 12, 17, 24);
@Test
public void testWillNotAcceptOutOfHours() {
assertFalse(callCenter.willAcceptCallback(currentTime, LocalDateTime.of(2021, 1, 12, 20, 15)));
}
@Test
public void testWillNotAcceptLessThanTwoHoursInFuture() {
assertFalse(callCenter.willAcceptCallback(currentTime, LocalDateTime.of(2021, 1, 12, 18, 26)));
}
@Test
public void testWillNotAcceptMoreThanSixWorkingDaysInFuture() {
assertFalse(callCenter.willAcceptCallback(currentTime, LocalDateTime.of(2021, 1, 18, 12, 1)));
}
}这是我在看测试场景时所知道的:
必须编写一个名为CallCenter的类,其中callCenter是对象引用。我们使用的是LocalDateTime类,它有一个名为currentTime的对象引用,它有todays和time参数值。CallCenter类有一个willAcceptCallBack方法。
我是测试驱动开发的新手,我该如何编写方法才能让测试通过呢?
public boolean willAcceptCallBack(currentTime, LocalDateTime())
{
// Potential scenarios:
// 1st write a scenario that will not accept out of hours calls
// 2nd write a scenario that will not accept calls less than 2 hours in the future
// 3rd write a scenario that will accept calls more than 6 days in the future
}提前感谢
发布于 2021-01-15 13:23:48
我相信真正的需求在于测试方法的名称。Like testWillNotAcceptOutOfHours意味着如果时间超过了小时数,willAcceptCallback()应该返回false。我希望您已经被告知了呼叫中心的开放时间?比如说,他们会在20:00结束吗?只是因为我从测试中读到20点15分不在工作时间。
您的方法应根据传递给它的日期和时间返回false或true。使用LocalDateTime的isBefore和/或isAfter方法。您可能还需要toLocalDate和toLocalTime方法。
https://stackoverflow.com/questions/65689505
复制相似问题