Skip to content

Instantly share code, notes, and snippets.

@gbzarelli
Last active October 29, 2022 23:43
Show Gist options
  • Save gbzarelli/cd083edc4506b5388a02899f508d25bf to your computer and use it in GitHub Desktop.
Save gbzarelli/cd083edc4506b5388a02899f508d25bf to your computer and use it in GitHub Desktop.
import com.tngtech.archunit.junit.AnalyzeClasses;
import com.tngtech.archunit.junit.ArchTest;
import com.tngtech.archunit.lang.ArchRule;
import static com.tngtech.archunit.library.Architectures.layeredArchitecture;
@AnalyzeClasses(packages = "br.com.helpdev.myapp")
class ArchitectureTest {
@ArchTest
static final ArchRule layer_dependencies_are_respected = layeredArchitecture()
.consideringAllDependencies()
.layer("Controller").definedBy("..controller..")
.layer("Service").definedBy("..service..")
.layer("Persistence").definedBy("..persistence..")
.whereLayer("Controller").mayNotBeAccessedByAnyLayer()
.whereLayer("Service").mayOnlyBeAccessedByLayers("Controller")
.whereLayer("Persistence").mayOnlyBeAccessedByLayers("Service")
}
@ArchTest
static ArchRule app_class_name_should_be_app =
classes().that()
.areAnnotatedWith(SpringBootApplication.class)
.should().haveSimpleName("MainApplication");
@ArchTest
static ArchRule controllers_should_be_suffixed =
classes().that()
.resideInAPackage("..controller..")
.or().areAnnotatedWith(RestController.class)
.should().haveSimpleNameEndingWith("Controller")
//Note that the allowEmptyShould(true) option allows tests to fail if the given project contains no controllers.
.allowEmptyShould(true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment