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.

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

@EventHandler
public void on(ToDoItemCreatedEvent event) {
this.id = 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:

@Test
public void canCreateToDoItemWithDeafultZeroProgress() throws Exception {
fixture.given()
.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

@EventHandler
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?

Thanks,

N

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.

Cheers,

Allard