如何测试不生成事件的状态存储聚合



我想使用AggregateTestFixture测试状态存储的聚合。但是我得到了AggregateNotFoundException: No 'given' events were configured for this aggregate, nor have any events been stored.错误。

我在命令处理程序中更改聚合的状态,并且不应用任何事件,因为我不希望我的域条目表不必要地增长。

这是我的聚合外部命令处理程序;

open class AllocationCommandHandler constructor(
private val repository: Repository<Allocation>,
) {
@CommandHandler
fun on(cmd: CreateAllocation) {
this.repository.newInstance {
Allocation(
cmd.allocationId
)
}
}
@CommandHandler
fun on(cmd: CompleteAllocation) {
this.load(cmd.allocationId).invoke { it.complete() }
}
private fun load(allocationId: AllocationId): Aggregate<Allocation> =
repository.load(allocationId)
}

这是总量;

@Entity
@Aggregate
@Revision("1.0")
final class Allocation constructor() {
@AggregateIdentifier
@Id
lateinit var allocationId: AllocationId
private set
var status: AllocationStatusEnum = AllocationStatusEnum.IN_PROGRESS
private set
constructor(
allocationId: AllocationId,
) : this() {
this.allocationId = allocationId
this.status = AllocationStatusEnum.IN_PROGRESS
}
fun complete() {
if (this.status != AllocationStatusEnum.IN_PROGRESS) {
throw IllegalArgumentException("cannot complete if not in progress")
}
this.status = AllocationStatusEnum.COMPLETED
apply(
AllocationCompleted(
this.allocationId
)
)
}
}

此聚合中没有AllocationCompleted事件的事件处理程序,因为它由其他聚合侦听。

这是测试代码;

class AllocationTest {
private lateinit var fixture: AggregateTestFixture<Allocation>
@Before
fun setUp() {
fixture = AggregateTestFixture(Allocation::class.java).apply {
registerAnnotatedCommandHandler(AllocationCommandHandler(repository))
}
}
@Test
fun `create allocation`() {
fixture.givenNoPriorActivity()
.`when`(CreateAllocation("1")
.expectSuccessfulHandlerExecution()
.expectState {
assertTrue(it.allocationId == "1")
};
}
@Test
fun `complete allocation`() {
fixture.givenState { Allocation("1"}
.`when`(CompleteAllocation("1"))
.expectSuccessfulHandlerExecution()
.expectState {
assertTrue(it.status == AllocationStatusEnum.COMPLETED)
};
}
}

create allocation测试通过,我在complete allocation测试中得到错误。

givenNoPriorActivity实际上不打算与State Stored聚合一起使用。最近对AggregateTestFixture进行了调整以支持这一点,但这将与Axon 4.6.0一起发布(当前最新版本为4.5.1(

然而,这并不能改变我觉得complete allocation测试失败很奇怪的事实。使用givenStateexpectState方法是可行的。也许Kotlin/Java组合现在正在发挥作用;你有没有尝试过用纯Java做同样的事情,只是为了确定?

无论如何,您共享的异常来自AggregateTestFixture中的RecordingEventStore。只有当事件源存储库实际上(由fixture(在后台使用时,才会发生这种情况,因为它将读取事件。可能是罪魁祸首,是givenNoPriorActivity的使用。请尝试将其替换为提供空聚合实例的givenState()

最新更新