3 回答
TA贡献1820条经验 获得超9个赞
我在测试中遇到了同样的问题,我想出的最佳解决方案是: Mock static method in Clock.systemUTC so it will return Clock.fixed()
try (MockedStatic<Clock> utilities = Mockito.mockStatic(Clock.class)) {
utilities.when(Clock::systemUTC)
.thenReturn(Clock.fixed(Instant.parse("2018-08-22T10:00:00Z"), ZoneOffset.UTC));
System.out.println(Instant.now()) //here perform actions in past
}
System.out.println(Instant.now()) // here perform in current time
TA贡献1735条经验 获得超5个赞
就我而言,配备的属性的数据类型@UpdateTimestamp是LocalDateTime. 我是这样解决的:
ShiftLog shiftLog1 = ShiftLog.builder().build();
ShiftLog shiftLog2 = ShiftLog.builder().build();
ShiftLog shiftLog3 = ShiftLog.builder().build();
LocalDateTime thePast = LocalDateTime.of(1979, 4, 3, 6, 45, 31);
try (MockedStatic<LocalDateTime> utilities = Mockito.mockStatic(LocalDateTime.class)) {
utilities.when(() -> LocalDateTime.now(ArgumentMatchers.any(Clock.class))).thenReturn(thePast);
repository.save(shiftLog1);
repository.save(shiftLog2);
}
// Now the @UpdateTimestamp is untouched again.
repository.save(shiftLog3);
添加回答
举报