Let's test that "references to other aggregates are made via Id".
@ArchTest
static ArchRule only_access_through_id_when_depend_on_another_aggregate_root =
classes()
.that().resideInAPackage("com.example.domain")
.and().haveSimpleName("HogeRoot")
.should().onlyAccessFieldsThat(new DescribedPredicate<JavaField>("not depend on another aggregate root object") {
@Override
public boolean apply(JavaField input) {
return !input.getClass().getSimpleName().equals("FugaRoot");
}
});
We are only testing that we are not directly referencing the aggregate route.
You can make it a little more strict by determining the field name like endWith ~ ("Id")
.
It is difficult to determine the aggregation route mechanically.
If you have something like @AggregateRoot
, you'll be happy.
Recommended Posts