AxonFramework icon indicating copy to clipboard operation
AxonFramework copied to clipboard

Possible bug in how `testFixture` loads aggregates

Open azzazzel opened this issue 4 years ago • 2 comments

It seems there may be a bug in how testFixture loads aggregates. The logs from the test below indicate the constructor is not called the first time aggregate is instantiated:

Starting test
 --> Got command: test
 ---> state: none
 --> Got event: test
 ---> current state: none
 ---> new state: none -> some
!!! New aggregate instance !!!
 --> Got event: test
 ---> current state: initial
 ---> new state: initial -> some

Basic information

  • Axon Framework version: 4.5.5
  • JDK version: 1.8.0_265-b01
  • Complete executable reproducer if available (e.g. GitHub Repo):
public class AggregateLoadingInFixture {

    static class MyCommand {
        @TargetAggregateIdentifier String id;
        public MyCommand(String id) { this.id = id; }
    }

    static class MyEvent {
        String id;
        public MyEvent(String id) { this.id = id; }
    }

    static class MyAggregate {

        @AggregateIdentifier String id = "";
        String state = "none";

        public MyAggregate() {
            System.out.println("!!! New aggregate instance !!!");
            state = "initial";
        }

        @CommandHandler
        public MyAggregate(MyCommand myCommand) {
            System.out.println(" --> Got command: " + myCommand.id);
            System.out.println(" ---> state: " + state);
            AggregateLifecycle.apply(new MyEvent(myCommand.id));
        }

        @EventSourcingHandler
        public void handle (MyEvent myEvent) {
            System.out.println(" --> Got event: " + myEvent.id);
            System.out.println(" ---> current state: " + state);
            state += " -> some";
            System.out.println(" ---> new state: " + state);
        }
    }

    @Test
    public void test() {
        System.out.println("Starting test");
        FixtureConfiguration<MyAggregate> testFixture = new AggregateTestFixture<>(MyAggregate.class);
        testFixture
                .givenNoPriorActivity()
                .when(new MyCommand("test"))
                .expectSuccessfulHandlerExecution();
    }
}

Steps to reproduce

Execute the test above

Expected behavior

The test passes

Actual behavior

The test fails with

org.axonframework.test.AxonAssertionError: Illegal state change detected! Property "org.axonframework.hct.AggregateLoadingInFixture$MyAggregate.state" has different value when sourcing events.
Working aggregate value:     <none -> some>
Value after applying events: <initial -> some>

The test passes if you remove the state = "initial"; line from the constructor.

azzazzel avatar Dec 22 '21 12:12 azzazzel

It turns out the issue is in the test itself. I'm sorry for the false alarm.

azzazzel avatar Dec 22 '21 17:12 azzazzel

No worries, @azzazzel! I'll close the issue then.

smcvb avatar Dec 23 '21 08:12 smcvb