I’m not able to guess how external command handlers are expected to be tested.
The primary reason for having an external command handler was that I have complex logic that is purely application/technical related, which I do not want to be polluting my aggregate.
I’m calling the execute method and passing a Consumer, so I have access to aggregate state properties that are essential do the calculations being executed. In the end, I pass the command back to the aggregate so the state can be modified.
I tried those different approaches:
Mock the Aggregate and Repository interfaces, which forces me to configure the load (straightforward) and execute methods, and execute the Consumer being passed by the command handler; I also have to provide the Aggregate itself
instantiate one (it complains about State not being defined)
or mock (which is not possible because I set my aggregate as final)- Use the AggregateTestFixture, obtain the repository from it, and pass the Repository to the command handler so that the Aggregate can be retrieved again from it.
It complains about unitOfWork not being started
It’s written in Docblock of the aggregate execute method that you are not supposed to be calling it, as you should not receive access to the Aggregate and not be able to manipulate its internals. I believe however that this advice is there mostly to avoid people from interacting it from the read side. As the command handlers that are in the aggregate have full access to the state itself, it felt natural to me that I could access this aggregate directly, at least for reading purposes.
I also think the documentation on the external command handler is very poor.