Domain / Event Handler Testing

Hi, please forgive me if this is a stupid question but I have a feeling I’m missing something fundamental.

I’m trying to apply some unit testing to the ToDoItem example. My ToDoItem domain object has the following command handler and event handler.

public ToDoItem(CreateToDoItemCommand command) {
apply(new ToDoItemCreatedEvent(command.getId(), command.getTitle(), command.getDescription(),
command.getAssignee(), command.getProgress()));

public void on(ToDoItemCreatedEvent event) { = event.getId();
this.title = event.getTitle();
this.description = event.getDescription();
this.assignee = event.getAssignee();
this.progress = event.getProgress();

The wiring of the commands and events can be tested successfully using:

public void canCreateToDoItemWithDeafultZeroProgress() throws Exception {
.when(new CreateToDoItemCommand(“todo1”, “First To Do Item”, “This is the first to do item.”,“Neil Dunlop”))
.expectEvents(new ToDoItemCreatedEvent(“todo1”, “First To Do Item”, “This is the first to do item.”,“Neil Dunlop”, 0));

My question is, how do I access the domain object to assert that the

public void on(ToDoItemCreatedEvent event) {

did all the right things?

I have tried to invoke the constructor directly using a command created within the test. Fairly obviously, an AxonAssertionError (“Illegal State Change Detected!”) is raised.

Is there any way to get access to the domain object from the fixture to assert that the handler modified the domain state in the correct way or am I fundamentally misunderstanding how things should be done?



Hi Neil,

using the fixtures, the only way to make sure the event handler method does the right job, is by making sure it is invoked in the “given” state. In other words, test the next step in your object’s lifecycle.

This type of testing ensures that everyting is defined in functional terms. The “how” things are done aren’t really relevant, as long as the correct behavior is shown.

Hope this clarifies a littlebit.