英文:
How can I test only classes with certain Spring profiles with Archunit
问题
我有一个使用不同配置文件(例如“dev”和“production”)的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")));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论