Codev
Codev

Reputation: 1150

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

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"?

Upvotes: 1

Views: 295

Answers (1)

matejko219
matejko219

Reputation: 1771

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")));

Upvotes: 1

Related Questions