【问题标题】:How can I test only classes with certain Spring profiles with Archunit如何使用 Archunit 仅测试具有某些 Spring 配置文件的类
【发布时间】:2020-07-23 08:38:37
【问题描述】:

我有一个具有不同配置文件的 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 Profile“生产”的类?

【问题讨论】:

    标签: java spring archunit


    【解决方案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")));
    
    

    【讨论】:

      猜你喜欢
      • 2021-01-16
      • 1970-01-01
      • 2014-10-27
      • 2019-07-29
      • 1970-01-01
      • 1970-01-01
      • 2016-02-25
      • 1970-01-01
      • 2019-05-25
      相关资源
      最近更新 更多