2

我想验证,给定包中的类仅指驻留在包本身中的类。但是我得到了违反,告诉我a类依赖于例如java.lang.String,这对我来说完全没问题。有没有办法忽略基本的java包?

        @ArchTest
        static final ArchRule zeroDependencyOfDomain = noClasses().that()
            .resideInAPackage("..domain..")
            .should().dependOnClassesThat()
            .resideOutsideOfPackages("..domain..");
4

2 回答 2

0

与其他答案类似,我也没有找到比这更好的方法:

private static final String[] COMMON_PACKAGES = {
    "java..",
    "javax..",
    "com.google..",
    "org.springframework.."};

@ArchTest
final ArchRule classesInProdutPackageNoOutsideAccess =
    classes()
        .that()
        .resideInAnyPackage(PACKAGE_FOO)
        .should()
        .onlyAccessClassesThat()
        .resideInAnyPackage(commonPackagesAnd(PACKAGE_FOO));


private static String[] commonPackagesAnd(String... packages) {
    return ArrayUtils.addAll(packages, COMMON_PACKAGES);
}
于 2019-07-23T15:42:24.113 回答
0

您可以显式地允许依赖于 java 包:

@ArchTest
static final ArchRule zeroDependencyOfDomain = noClasses().that()
    .resideInAPackage("..domain..")
    .should().dependOnClassesThat()
    .resideOutsideOfPackages("..domain..", "java..");

您也可以使用classes()and onlyDependOnClassesThat(),在我看来这更容易阅读:

@ArchTest
static final ArchRule zeroDependencyOfDomain = classes().that()
    .resideInAPackage("..domain..")
    .should().onlyDependOnClassesThat()
    .resideInAnyPackage("..domain..", "java..");
于 2019-07-22T14:26:28.493 回答