[英]How to check that a constructor is called in the right classes with ArchUnit?
有沒有辦法用 ArchUnit 強制執行這樣的規則:
@ArchTest
static final ArchRule events_must_be_created_by_aggregates =
noConstructors().that().areDeclaredInClassesThat().areAssignableTo(Event.class).should().beCalledInClassesThat().areNotAssignableFrom(Aggregate.class)
.because("the aggregate should manage its own lifecycle and events");
這里的問題是beCalledInClassesThat
不存在,我找不到任何可以讓我實現這樣一個測試的東西。
每當您錯過預定義的 fluent API 中的某些內容時,請嘗試定義自定義謂詞/條件。 在您的情況下:這對您有用嗎?
@ArchTest
static final ArchRule events_must_be_created_by_aggregates = constructors()
.that().areDeclaredInClassesThat().areAssignableTo(Event.class)
.should(new ArchCondition<JavaConstructor>("be called from aggregates") {
@Override
public void check(JavaConstructor constructor, ConditionEvents events) {
for (JavaConstructorCall call : constructor.getCallsOfSelf()) {
if (!call.getOriginOwner().isAssignableTo(Aggregate.class)) {
events.add(SimpleConditionEvent.violated(call, call.getDescription()));
}
}
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.