Search code examples
kotlintestingarcharchunit

How to exclude specific classes with ArchUnit tests?


I currently have a simple ArchUnit test that checks that all classes that reside in a package should only be accessed within that class.

However, I want to exclude one specific class from that rule and I can't seem to figure out a way to do so.

     val rule: ArchRule =
         classes().that().resideInAPackage("$base..").should().onlyBeAccessed().byClassesThat()
           .resideInAPackage("$base..")
     rule.check(importedClasses)

However I want to exclude $base.myClass from the rule but it all seems to work with packages rather than classes.


Solution

  • This should work:

        val rule: ArchRule =
            classes()
                .that().resideInAPackage("$base..").and().doNotHaveFullyQualifiedName("$base.myClass")
                .should().onlyBeAccessed().byClassesThat().resideInAPackage("$base..");