Skip to content

Add assertion support on @EventSourcedEntity in the AxonTestFixture #4249

Description

@theoema

When Event Modeling we frequently model automations, and when making specifications for automations we typically do it the same way we specify state views, and specify state changes. By defining Given (events) Then (readmodel), and Given (events) When (command), Then (event(s)).

Automations are typically implemented as a readmodel that is streaming to or being polled by a concurrency-limited / single-threaded processor. But Axon Framework 5 introduced a different way of implementing automations, that introduces significant concurrency benefits.

We dont want our implementation details to change the way we model, so that got me thinking about how to create automations in AF5 in a way that maps really cleanly to the specifications. Now I've also been playing a lot with static codegen from my event models into AF5 code, and one of the best features is being able to statically generate tests from specifications with the help of the AxonTestFixture. Having those tests in place is a real success factor when it comes to letting AI implement slices. But when I generate automations I found myself needing to statically generate "readmodel assertions" on @EventSourcedEntity classes.

Heres how I currently do it:

fixture.given()
       .event(new StudentCreatedEvent("student-1", "Alice"))
       .then()
       .events(new StudentNameChangedEvent("student-1", "Bob"))
       .expect(config -> {
           var stateManager = config.getComponent(StateManager.class);
           var uowFactory = config.getComponent(UnitOfWorkFactory.class);
           var processingContext = uowFactory.create().processingContext();
           var student = stateManager.loadEntity(Student.class, "student-1", processingContext).join();
           assertThat(student.name()).isEqualTo("Bob");
       });

But I think that the test fixture would benefit from a "cleaner" api regarding this, something like this:

fixture.given()
       .event(new StudentCreatedEvent("student-1", "Alice"))
       .then()
       .events(new StudentNameChangedEvent("student-1", "Bob"))
       .entity(Student.class, "student-1", student -> {
           assertThat(student.name()).isEqualTo("Bob");
       });

Would love to know what you guys think, I could definitely make a PR for it, but I wanted to discuss it first.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Priority 2: ShouldHigh priority. Ideally, these issues are part of the release they’re assigned to.Type: FeatureUse to signal an issue is completely new to the project.

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions