Unit Testing – Are DDD Aggregates Good Boundaries?

domain-driven-designtddunit testing

An aggregate can represent a somewhat stable system boundary.

Does it make sense to write input and output test aggregates to do most testing? In theory we could use an aggregate as a data driven way to do testing. We can create data that is going into our system without a database layer and we get a processed aggregate out. We could potentially specify a lot of cases this way.

The risk here is that we are limiting our ability to modify the aggregates because we may need to rewrite all our test aggregates if an aggregate changes. Although we could migrate them.

Best Answer

Aggregates are definitely good candidates for unit testing because

  • They contain part of the domain logic of your application. There's intelligence in them (invariant checking, state transitions, calculations, event firing, etc.) that you want to be correct.

  • Aggregates are supposed to be devoid of I/O related stuff. You can compose an Aggregate in memory, call one of its methods to test it and everything will be fast and isolated from the other layers.

Functional changes are IMO not a valid argument against unit testing aggregates - or against any kind of tests for that matter. Tests are executable specification. If the requirements changes, some tests have to change.

Related Topic