将服务器日期时间迫使给定值,以执行没有新日期作为参数的JUNIT测试

发布于 2025-01-30 00:40:19 字数 195 浏览 2 评论 0原文

我如何在我的Java应用程序的单元测试中模拟某个DateTime,独立于服务器的实际日期时间,以便该应用程序“相信”当前的日期时间是我强制的i,并且可以执行测试'现在我强制的日期时间等于吗?

PS:这个问题不是从其他问题中重复的

assertThat(newDate).isEqualTo(someDate);

How can I simulate a certain datetime inside a unit test of my java application, independent from the real date time of the server, such that the application 'believes' that the current datetime is the one I forced and the test can be executed 'as' now would be equal to the datetime I forced?

PS: The question is not duplicated from other question because my test has not the new datetime as parameter, in other words is not something like

assertThat(newDate).isEqualTo(someDate);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

允世 2025-02-06 00:40:19

使用依赖注入将某些内容传递到应用程序中,使您可以获取日期:

public class Application {
    public Application(Calendar calendar) {
        this.calendar = calendar;
    }

    public void doSomething() {
        LocalDate today = calendar.todaysDate();
        // Do something based on the date
    }

    private Calendar calendar;
}

其中calendar只是一个抽象:

import java.time.LocalDate;

public interface Calendar {
    public LocalDate todaysDate();
}

对于测试,您可以具有返回所需日期的实现:

public class FixedDateCalendar implements Calendar {
    @Override
    public LocalDate todaysDate() {
        return LocalDate.of(2022, 5, 1);
    }
}

然后将其传递到应用程序:

Application myApp = new Application(new FixedDateCalendar());

在生产代码中,您需要一个使用系统时钟的实现:

public class SystemCalendar implements Calendar {
    @Override
    public LocalDate todaysDate() {
        return LocalDate.now();
    }
}

同样,这只是有线的:

Application myApp = new Application(new SystemCalendar());

Use dependency injection to pass something into the application that lets you get the date:

public class Application {
    public Application(Calendar calendar) {
        this.calendar = calendar;
    }

    public void doSomething() {
        LocalDate today = calendar.todaysDate();
        // Do something based on the date
    }

    private Calendar calendar;
}

where Calendar is just an abstraction:

import java.time.LocalDate;

public interface Calendar {
    public LocalDate todaysDate();
}

For tests, you can have an implementation that returns the date you want:

public class FixedDateCalendar implements Calendar {
    @Override
    public LocalDate todaysDate() {
        return LocalDate.of(2022, 5, 1);
    }
}

and you pass it into the application:

Application myApp = new Application(new FixedDateCalendar());

In the production code, you want an implementation that uses the system clock:

public class SystemCalendar implements Calendar {
    @Override
    public LocalDate todaysDate() {
        return LocalDate.now();
    }
}

and again, this is just wired up:

Application myApp = new Application(new SystemCalendar());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文