Konsist's Architectural Checks serve as a robust tool for maintaining layer isolation, enabling development teams to enforce strict boundaries between different architectural layers. Here few things that can be verified with Konsist:
Let's write a simple test to verify that application architecture rules are preserved. In this scenario, the application follows a simple 3-layer architecture, where Presentation and Data layers depend on Domain layer and Domain layer is independant (from these layers):
Overview
On a high level writing Konsist architectural check requires 3 steps:
Let's take a closer look at each of these steps.
1. Define Layers
Create layers instances to represent project layers. Each Layer instance accepts the name (used for presenting architecture violation errors) and package used to define layers.
The double dot syntax (..) means zero or more packages - layer is represented by the package and all of it's sub packages (seePackage Wildcard syntax).
2. Create The Scope
The Konsist object is an entry point to the Konsist library.
Konsist
The scopeFromX methods obtains the instance of the scope containing Kotlin project files. To get all Kotlin project files present in the project use the scopeFromProject method:
// Define layersprivateval presentationLayer =Layer("Presentation", "com.myapp.presentation..")privateval domainLayer =Layer("Domain", "com.myapp.domain..")privateval dataLayer =Layer("Data", "com.myapp.data..")// Define the scope containing all Kotlin files present in the projectKonsist.scopeFromProject() //Returns KoScope
To define more granular scopes such as scope from production code or scope from single module see the Create The Scope page.
3. Assert Architecture
To performa assertion use the assertArchiteture method:
The declaration validation logic should be protected through automated testing. By wrapping Konsist checks within standard testing frameworks such as JUnit or KoTest, you can verify these rules with each Pull Request:
For Kotest to function correctly the Kotest test name has to be explicitly passed. See theKotest Support page.
The Kotest testing framework project dependency should be added to the project. See starter projects to get a complete sample project.
Note that test class has a KonsistTest suffix. This is the recommended approach to name classes containing Konsist tests.
Summary
This section described the basic way of writing Konsist architectural test. To get a better understanding of how Konsist API works see Debug Konsist Test.