相关疑难解决方法(0)

模拟 Instant.now() 而不使用 Clock 进入构造函数或不使用 Clock 对象

我的方法之一中有以下代码

ZonedDateTime current = Instant.now().atZone(ZoneId.of(AMERICA_NEW_YORK));
Run Code Online (Sandbox Code Playgroud)

我想current在 JUnit 测试中进行模拟。

我尝试过,java.time.Clock但为此,我需要将其添加到类构造函数中,因为我的代码写入旧版本的 Spring 并使用基于 XML 的配置,此类会导致问题,因为它需要 application-context.xml 文件中的构造函数参数,如果我使用构造函数与Clock.

有没有办法避免current上面代码中的构造函数配置和模拟。

更新

根据帕维尔·斯米尔诺夫的评论,我在下面尝试过,但current仍然返回今天的日期,但不是我嘲笑的日期。

ZonedDateTime exactOneDay = ZonedDateTime.parse("Sun Oct 21 12:30:00 EDT  2018", Parser); 
doReturn(exactOneDay).when(spyEmployeeHelper).getCurrentTime();
employee = getEmployees().get(0);
assertEquals(Integer.valueOf(1), employee.getNoticePeriod());
Run Code Online (Sandbox Code Playgroud)

java junit java-8

14
推荐指数
2
解决办法
4万
查看次数

标签 统计

java ×1

java-8 ×1

junit ×1