学习Java测试驱动开发



我正在学习使用JUnit 4的Java测试驱动开发。我已经得到了一个测试场景,并且必须编写实现代码,以使测试通过

这是测试场景:

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的对象引用,它的参数值为today日期和时间。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
}

提前感谢

我相信真正的需求在测试方法名称中。类似testWillNotAcceptOutOfHours意味着如果时间超过小时,willAcceptCallback()应该返回false。我希望你已经被告知呼叫中心的开放时间?例如,它们在20:00结束吗?只是因为我从测试中读到20:15是非工作时间。

您的方法应根据传递给它的日期和时间返回falsetrue。请使用LocalDateTimeisBefore和/或isAfter方法。您可能还需要toLocalDatetoLocalTime方法。

最新更新