英文:
How to change the internal state of a Clock instance in Java?
问题
我有一个用于验证日期过期的服务:
@RequiredArgsConstructor
public class ExpirationVerificationService {
private final Clock clock;
public boolean hasPassed(Instant instant) {
return Instant.now(clock).isAfter(instant);
}
}
我想要验证hasPassed
方法随着时间的推移返回不同的值:
public class ExpirationVerificationServiceTest {
private ExpirationVerificationService service;
private Clock clock;
@BeforeEach
public void init() {
clock = Clock.fixed(Instant.EPOCH, ZoneId.of("UTC"));
service = new ExpirationVerificationService(clock);
}
@Test
public void testHasExpired() {
Instant instant = Instant.now(clock).plus(Duration.ofDays(30));
assertFalse(service.hasPassed(instant));
// TODO 将时钟调整到未来
assertTrue(service.hasPassed(instant));
}
}
如何更新Clock
实例的内部状态以使其处于未来状态?
注:我正在测试的实际业务逻辑要比这个示例复杂得多(验证来自数据库的 OAuth 令牌的过期),我不能只是在过去使用不同的Instant
实例。
英文:
I have a service that verifies expiration of dates:
@RequiredArgsConstructor
public class ExpirationVerificationService {
private final Clock clock;
public boolean hasPassed(Instant instant) {
return Instant.now(clock).isAfter(instant);
}
}
I want to verify that hasPassed
returns different values as time passes:
public class ExpirationVerificationServiceTest {
private ExpirationVerificationService service;
private Clock clock;
@BeforeEach
public void init() {
clock = Clock.fixed(Instant.EPOCH, ZoneId.of("UTC"));
service = new ExpirationVerificationService(clock);
}
@Test
public void testHasExpired() {
Instant instant = Instant.now(clock).plus(Duration.ofDays(30);
assertFalse(service.hasPassed(instant));
// TODO move clock to future
assertTrue(service.hasPassed(instant));
}
}
How can I update the internal state of the Clock
instance to be in the future?
note: the actual business logic I'm testing is much more complicated than this example (verifying expiration of Oauth tokens coming from a DB), I can't just use a different Instant
instance in the past.
答案1
得分: 2
Clock
实际上只是 Instant
的提供者。您可以轻松地声明一个固定的时钟,如下所示:
Instant fixedInstant = Instant.EPOCH;
Clock clock = () -> fixedInstant;
因此,如果您想要一个可设置的 Clock
,您可以这样声明:
AtomicReference<Instant> theInstant = new AtomicReference<>(Instant.EPOCH);
Clock clock = () -> theInstant.get();
然后更新 theInstant
。
英文:
Clock
is effectively just a provider of an Instant
. You can trivially declare a fixed clock like:
Instant fixedInstant = Instant.EPOCH;
Clock clock = () -> fixedInstant;
As such, if you want a settable Clock
, you could declare:
AtomicReference<Instant> theInstant = new AtomicReference<>(Instant.EPOCH);
Clock clock = () -> theInstant.get();
and then update theInstant
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论