Search code examples
javagenericsarchunit

How to check dependencies in generics with ArchUnitTest?


We have a rule that package "foo" should not depend on package "bar"

noClasses()
                .that()
                .resideInAPackage("com.foo..")
                .should()
                .dependOnClassesThat(resideInAPackage("com.bar.."))
                .check(javaClasses);

And we have an interface in "foo" using "bar" in two ways, directly and via generics.

public interface IFoo {

    BarClass getBar();

    Optional<BarClass> getOptionalBar();

    List<BarClass> getListBar();
}

The "BarClass getBar();" is caught by ArchUnitTest, but "Optional getOptionalBar()" and "List getListBar()" doesn't give us any error in the test. Does anybody know how to make them failing too?


Solution

  • ArchUnit 0.20.0 has introduced support for generic return types. With the current version, type arguments of generic method return types should automatically be considered as class dependencies.