如何测试依赖于时间的聚合?



我正在尝试对计时应用程序进行建模。

通常,当我有一个依赖于时间的类时,我可以提供一个重载的构造函数或方法,以便能够将 Clock 注入该方法或类并能够测试其行为。

如果我有一个命令需要能够将当前时间传递到事件中,那么这如何在基于轴突的应用程序的聚合中工作?

@Aggregate
@Slf4j
public class TimeCard {
@AggregateIdentifier
private String employeeName;
private Instant clockInTime;
public TimeCard() {
//Axon requires empty constructor on aggregate
}
@CommandHandler
public TimeCard(ClockInCommand cmd) {
AggregateLifecycle.apply(new ClockInEvent(cmd.getEmployeeName(), Instant.now()));
}
@EventSourcingHandler
public void on(ClockInEvent event) {
this.employeeName = event.getEmployeeName();
this.clockInTime = event.getClockInTime();
}
}

似乎测试夹具通过提供提供时间的方法为我处理得很干净。这是我的测试方法:

@Test
void testClockInCommand() {
testFixture.givenNoPriorActivity()
.andGivenCurrentTime(clock.instant())
.when(new ClockInCommand("GoldFlsh"))
.expectEvents(new ClockInEvent("GoldFlsh", testFixture.currentTime()));
}

但我的活动最终确实偏离了几分之一秒。

Expected <2020-02-02T13:47:20.684344700Z> but got <2020-02-02T13:47:20.954347700Z>

处理这个问题的最佳方法是什么?命令应该只从上游获取时间吗?或者我可以以某种方式注入一个时钟进行测试。

当依赖聚合(和其他轴突类型(中的时间时,您可以使用在大多数运行时配置中默认为 System.UTC 的GenericEventMessage.clock

测试夹具将在测试期间将其覆盖为固定时间。更新 Instant.now(( 的使用以使用此时钟。

@CommandHandler
public TimeCard(ClockInCommand cmd) {
AggregateLifecycle.apply(new ClockInEvent(cmd.getEmployeeName(), GenericEventMessage.clock.instant()));
}

最新更新