gpt4 book ai didi

使用 new Date() 作为当前日期的 Java 单元测试方法

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:51:02 24 4
gpt4 key购买 nike

所以我有一个类有一个方法“getDaysUntil(Date date)”,它返回作为参数给出的日期之前的天数。我提到我无法更改以下类(class):

public class A {

public int getDaysUntil(Date givenDate) {

... // code

Date currentDate = new Date() //it creates a date object holding the current day

...// code that calculates the nr of days between currentDate and givenDate.

}

我必须做一些单元测试,您可能会发现问题所在,它在方法内部创建了 currentDate,返回值每天都会不同。我试图用 PowerMock 模拟 Date 对象或“覆盖”System.currentTimeMillis() 但无济于事。

有什么方法可以正确测试这些方法吗?

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