archunit

Archunit layer that consists of everyting that is not other layers


I am slowly starting to write archunit tests for my project. As of now only some parts of my architecture can be checked by it. For example I have a new package, that has clean dependencies.


ArchRule x = layeredArchitecture()
    .consideringOnlyDependenciesInAnyPackage("org.mycompany..")
    .layer("newModule").definedBy("org.mycompany.newmodule..")
    .layer("common").definedBy("org.mycompany.common..")
    .layer("apiclient").definedBy("org.mycompany.apiclient..")
    .layer("rest").definedBy(resideInAPackage("org.mycompany..").and(
        not(resideInAPackage("org.mycompany.newmodule..")
            .or(resideInAPackage("pl.vig.eclaims.common.."))
            .or(resideInAPackage("org.mycompany.apiclient.."))
        )))
     .whereLayer("newModule").mayNotBeAccessedByAnyLayer()
     .whereLayer("newModule").mayOnlyAccessLayers("common", "apiclient");

I want to ensure that my newModule only uses common and api client and no other my code and that noone uses my newModule.

In future I'll be adding new similar modules, but I would like to be able to not update the "rest" layer. Maybe I could define it as layer that has everyting except other layers, but I can't find any api for that. Or maybe I should approach this from another angle?


Solution

  • The rest does not really sound like a layer. 😅

    I wouldn't use the LayeredArchitecture from the library API, but stick to the lang API in this case:

    ArchRule newModule_may_only_depend_on_common_and_apiclient =
        noClasses()
        .that().resideInAPackage("org.mycompany.newmodule..")
        .should().dependOnClassesThat(
            resideInAPackage("org.mycompany..")
                .and(doNot(resideInAnyPackage("org.mycompany.common..", "org.mycompany.apiclient..")))
        );
    
    ArchRule newModule_is_not_used_externally =
        noClasses()
        .that().resideOutsideOfPackage("org.mycompany.newmodule..")
        .should().dependOnClassesThat().resideInAnyPackage("org.mycompany.newmodule..");