Skip to content

Instantly share code, notes, and snippets.

@andres-sacco
Created January 16, 2021 00:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andres-sacco/6429fc96d58b43244b1e9bfb78be4433 to your computer and use it in GitHub Desktop.
Save andres-sacco/6429fc96d58b43244b1e9bfb78be4433 to your computer and use it in GitHub Desktop.
Layer architecture
import static com.tngtech.archunit.library.Architectures.layeredArchitecture;
import com.tngtech.archunit.core.importer.ImportOption;
import com.tngtech.archunit.junit.AnalyzeClasses;
import com.tngtech.archunit.junit.ArchTest;
import com.tngtech.archunit.lang.ArchRule;
@AnalyzeClasses(packages = "com.example.archunit", importOptions = ImportOption.DoNotIncludeTests.class)
class LayeredArchitectureTest {
private static final String CONTROLLER = "Controller";
private static final String REPOSITORY = "Repository";
private static final String SERVICE = "Service";
@ArchTest
static final ArchRule layer_dependencies_are_respected = layeredArchitecture()
.layer(CONTROLLER).definedBy("..controller..")
.layer(SERVICE).definedBy("..service..")
.layer(REPOSITORY).definedBy("..repository..")
.whereLayer("Controllers").mayNotBeAccessedByAnyLayer()
.whereLayer(SERVICE).mayOnlyBeAccessedByLayers(CONTROLLER)
.whereLayer(REPOSITORY).mayOnlyBeAccessedByLayers(SERVICE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment