如何仅使用 Archunit 测试具有特定 Spring 配置文件的类。

huangapple go评论72阅读模式
英文:

How can I test only classes with certain Spring profiles with Archunit

问题

我有一个使用不同配置文件例如devproduction”)的Spring应用程序我使用Archunit来测试架构我有如下测试

    @Test
    public void Services_should_only_be_accessed_by_Controllers() {
        JavaClasses importedClasses = new ClassFileImporter().importPackages("com.mycompany.myapp");

        ArchRule myRule = classes()
            .that().resideInAPackage("..service..")
            .should().onlyBeAccessed().byAnyPackage("..controller..", "..service..");

        myRule.check(importedClasses);
    }

我的包中的类具有不同的配置文件如何只包括具有Spring配置文件production的类
英文:

I have a Spring application with different profiles e.g. "dev" and "production". I test the architecture with Archunit. I have tests like

@Test
public void Services_should_only_be_accessed_by_Controllers() {
    JavaClasses importedClasses = new ClassFileImporter().importPackages("com.mycompany.myapp");

    ArchRule myRule = classes()
        .that().resideInAPackage("..service..")
        .should().onlyBeAccessed().byAnyPackage("..controller..", "..service..");

    myRule.check(importedClasses);
}

The classes in my package have different profiles. How can I only include classes with the Spring Profile "production"?

答案1

得分: 1

JavaClasses importedClasses = new ClassFileImporter().importPackages("com.mycompany.myapp")
                .that(DescribedPredicate.describe("profile", clazz -> 
                        clazz.isAnnotatedWith(Profile.class) &&
                                Arrays.asList(clazz.getAnnotationOfType(Profile.class).value()).contains("production")));
英文:
JavaClasses importedClasses = new ClassFileImporter().importPackages("com.mycompany.myapp")
                .that(DescribedPredicate.describe("profile", clazz -> 
                        clazz.isAnnotatedWith(Profile.class) && 
                                Arrays.asList(clazz.getAnnotationOfType(Profile.class).value()).contains("production")));

huangapple
  • 本文由 发表于 2020年7月23日 16:38:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63050213.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定