ArchUnit rule to check Integration Test location

43 Views Asked by At

I want to write an ArchUnit rule that check that classes having a name ending with IT must be located in the ingreationTest module.

Looking at the API it seems that is only possible to check the package and not the folder/module where a class is located.

I'm using gradle.

Thanks

1

There are 1 best solutions below

0
Manfred On BEST ANSWER

It's not entirely clear to me what to "be located in the ingreationTest module" means to you, but can probably make use of JavaClass's getSource() method, which tells where this class has been imported from.

For example:

classes()
    .that().haveSimpleNameEndingWith("IT")
    .should(be(describe("located in the 'ingreationTest' module", javaClass ->
        javaClass.getSource()
            .map(source -> source.getUri().getPath().contains("ingreationTest"))
            .orElse(false)
    )))

I'm using the following static imports:

import static com.tngtech.archunit.base.DescribedPredicate.describe;
import static com.tngtech.archunit.lang.conditions.ArchConditions.be;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;