Hi,
I am having difficulties testing my command handler as the aggregate root in test, relies on another aggregate root for instantiation. I’m sure im not the first problem to have this problem so I am probably missing something, my code is as follows:
`
public class DayTicketWater extends AbstractAnnotatedAggregateRoot {
public DayTicketWater(DayTicketWaterId id, String name) {
this.id = id;
apply(new DayTicketWaterBuilt(id, name));
}
public DayTicketSession scheduleSession(DayTicketSessionId sessionId, SessionPeriod period) {
return new DayTicketSession(sessionId, this.id, period, this.pegs);
}
}
public class DayTicketSession extends AbstractAnnotatedAggregateRoot {
@AggregateIdentifier
private DayTicketSessionId id;
private ArrayList availablePegs = new ArrayList<>();
@SuppressWarnings(“UnusedDeclaration”)
DayTicketSession() {}
public DayTicketSession(DayTicketSessionId id, DayTicketWaterId waterId, SessionPeriod period, ArrayList pegs) {
apply(new DayTicketSessionScheduled(id, waterId, period, pegs));
}
}
public class DayTicketSessionCommandHandler {
private Repository repository;
private Repository waterRepository;
@Autowired
public DayTicketSessionCommandHandler(Repository repository, Repository waterRepository) {
this.repository = repository;
this.waterRepository = waterRepository;
}
@CommandHandler
public void handle(ScheduleDayTicketSession command) {
DayTicketWater water = waterRepository.load(command.getWaterId());
DayTicketSession session = water.scheduleSession(command.getId(), command.getPeriod());
repository.add(session);
}
}
public class DayTicketSessionCommandHandlerTest {
private FixtureConfiguration fixture;
private FixtureConfiguration waterFixture;
@Before
public void setUp() {
fixture = Fixtures.newGivenWhenThenFixture(DayTicketSession.class);
waterFixture = Fixtures.newGivenWhenThenFixture(DayTicketWater.class);
DayTicketSessionCommandHandler commandHandler = new DayTicketSessionCommandHandler(fixture.getRepository(), waterFixture.getRepository());
fixture.registerAnnotatedCommandHandler(commandHandler);
}
@Test
public void it_should_schedule_session_with_pegged_water() {
DayTicketSessionId id = DayTicketSessionId.generate();
SessionPeriod sessionPeriod = new SessionPeriod(LocalDate.of(2010, 10, 1), LocalDate.of(2010, 10, 2));
DayTicketWaterId waterId = DayTicketWaterId.generate();
Peg peg = new Peg(“The Shallows”);
waterFixture.given(
new DayTicketWaterBuilt(waterId, “Fishery 1”),
new PegAddedToDayTicketWater(waterId, peg)
);
fixture.given()
.when(new ScheduleDayTicketSession(id, waterId, sessionPeriod))
.expectVoidReturnType()
.expectEvents(new DayTicketSessionScheduled(id, waterId, sessionPeriod, new ArrayList<>(Arrays.asList(peg))));
}
}
`
As you can see the DayTicketSession is instantiated by DayTicketWater when invoking the scheduleSession method. I obviously want to add tests for this, if you check my DayTicketSessionCommandHandlerTest, this is where the problem is. The error is as follows:
The aggregate loaded based on the generated events seems to be of another type than the original. Working type: <DayTicketWater> Event Sourced type: <DayTicketSession>