【发布时间】:2021-08-16 15:38:25
【问题描述】:
我想使用 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 测试中得到错误。
【问题讨论】:
标签: spring-boot hibernate kotlin jpa axon