Skip to content

Instantly share code, notes, and snippets.

Avatar

Kirshi Yin kirshiyin89

View GitHub Profile
View ArchUnitTest.java
@AnalyzeClasses(packages = "org.mypackage", importOptions = {ArchUnitTest.ExcludeControllerImportOption.class, ImportOption.DoNotIncludeTests.class})
public class ArchUnitTest {
static class ExcludeControllerImportOption implements com.tngtech.archunit.core.importer.ImportOption {
@Override
public boolean includes(Location location) {
return !location.contains("SomeControllerClassThatNeedsToBeExcluded");
}
}
@kirshiyin89
kirshiyin89 / ArchUnitTest.java
Last active Jul 15, 2022
exclude tests from archunit
View ArchUnitTest.java
@AnalyzeClasses(packages = "org.mypackage", importOptions = ImportOption.DoNotIncludeTests.class)
View ArchUnitTest.java
@ArchTest
static final ArchRule onion_architecture_is_respected = onionArchitecture()
.domainModels("..domain.model..")
.domainServices("..domain.service..")
.applicationServices("..application..")
.adapter("cli", "..adapter.cli..")
.adapter("persistence", "..adapter.persistence..")
.adapter("rest", "..adapter.rest..");
View ArchUnitTest.java
@ArchTest
static ArchRule configs_should_be_annotated =
classes()
.that().resideInAPackage("..config..")
.and().areNotNestedClasses()
.should().beAnnotatedWith(Configuration.class)
.orShould().beAnnotatedWith(ConfigurationProperties.class);
View MyArchUnitTest.java
@ArchTest
static final ArchRule xml_requests_must_reside_in_request_package =
classes().that().areAssignableTo(SoapRequest.class)
.should().resideInAPackage("..request..")
.as("Xmls requests should reside in a package '..request..'");
@kirshiyin89
kirshiyin89 / ArchUnitTest.java
Last active Jul 4, 2022
configs_package_test
View ArchUnitTest.java
@ArchTest
static final ArchRule configs_must_reside_in_a_config_package =
classes().that().areAnnotatedWith(Configuration.class)
.or().areNotNestedClasses().and().areAnnotatedWith(ConfigurationProperties.class)
.should().resideInAPackage("..config..")
.as("Configs should reside in a package '..config..'");
@kirshiyin89
kirshiyin89 / ArchUnitTest.java
Last active Jul 4, 2022
entities_package_test
View ArchUnitTest.java
@ArchTest
static final ArchRule entities_must_reside_in_a_model_package =
classes().that().areAnnotatedWith(Entity.class)
.should().resideInAPackage("..model..")
.as("Entities should reside in a package '..model..'")
.allowEmptyShould(true);
@kirshiyin89
kirshiyin89 / ArchUnitTest.java
Last active Jul 4, 2022
controllers_suffix_test
View ArchUnitTest.java
@ArchTest
static ArchRule controllers_should_be_suffixed =
classes()
.that().resideInAPackage("..controller..")
.or().areAnnotatedWith(RestController.class)
.should().haveSimpleNameEndingWith("Controller")
.allowEmptyShould(true);
View ArchUnitTest.java
@AnalyzeClasses(packages = "org.mypackage")
@ArchTest
static ArchRule app_class_name_should_be_app =
classes().that().areAnnotatedWith(SpringBootApplication.class)
.should().haveSimpleName("MainApp");
View LayeredArchitecture.java
@ArchTest
static final ArchRule layer_dependencies_are_respected = layeredArchitecture()
.layer("Controllers").definedBy("com.tngtech.archunit.example.layers.controller..")
.layer("Services").definedBy("com.tngtech.archunit.example.layers.service..")
.layer("Persistence").definedBy("com.tngtech.archunit.example.layers.persistence..")
.whereLayer("Controllers").mayNotBeAccessedByAnyLayer()
.whereLayer("Services").mayOnlyBeAccessedByLayers("Controllers")
.whereLayer("Persistence").mayOnlyBeAccessedByLayers("Services");