0

有没有办法使用 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不存在,我找不到任何可以让我实施这样的测试的东西。

4

1 回答 1

1

每当您错过预定义的 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()));
                }
            }
        }
    });
于 2021-10-26T19:26:49.413 回答