robbit
robbit

Reputation: 427

Test package dependencies within a module with ArchUnit

I have trouble formulating the following test with ArchUnit:

I want to ensure that all classes in a certain package only access classes outside of the application base package or within a certain sub package ("or" not "xor").

What I have got is:

@AnalyzeClasses(packages = "com.example")
public class ClassDependencies {
    @ArchTest
    static final ArchRule deps = classes.that()
        .resideInAPackage("com.example.mymodule.some.package")
        .should().onlyAccessClassesThat().resideOutsideOfPackage("com.example..")
        .orShould().resideInAnyPackage("com.example.package1..", "com.example.package2..);

Problem is, that the or-condition should be within the onlyAccessClassesThat(). Above formulation fails if a class has both types of access, which I want to be valid.

How can I achieve what I want? Thanks for any help on this...

Upvotes: 0

Views: 1312

Answers (1)

nrainer
nrainer

Reputation: 2623

You can specify provide the predicate as method argument and combine it with another predicate:

classes
    .that().resideInAPackage("com.example.mymodule.some.package")
    .should().onlyAccessClassesThat(
        JavaClass.Predicates.resideOutsideOfPackage("com.example..")
        .or(JavaClass.Predicates.resideInAnyPackage("com.example.package1..", "com.example.package2.."))
    )

Upvotes: 2

Related Questions