I have a class that has three methods that shouldn't be called from certain classes.
How can I check this with Archunit?
So for example
public class Foo {
public void method1() {
}
public void method2() {
}
public void method3() {
}
}
method1 and method2 should only be called by classes Bar1 and Bar2.
With
import com.tngtech.archunit.lang.ArchRule;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses;
import static com.tngtech.archunit.lang.conditions.ArchConditions.callMethod;
you can use
ArchRule rule = noClasses()
.that().doNotHaveFullyQualifiedName(Bar1.class.getName())
.and().doNotHaveFullyQualifiedName(Bar2.class.getName())
// alternative 1:
.should().callMethod(Foo.class, "method1")
.orShould().callMethod(Foo.class, "method2");
// alternative 2:
// .should(callMethod(Foo.class, "method1").or(callMethod(Foo.class, "method2")));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With