Konsist is a linter that guards the consistency of Kotlin projects by enforcing a cohesive code structure and unified architecture. Konsist guards are written in the form of unit tests (JUnit / Kotest).
// Gradle Kotlin:
testImplementation("com.lemonappdev:konsist:0.16.1")
// Gradle Groovy:
testImplementation "com.lemonappdev:konsist:0.16.1"
// Maven:
<dependency>
<groupId>com.lemonappdev</groupId>
<artifactId>konsist</artifactId>
<version>0.16.1</version>
<scope>test</scope>
</dependency>
Check the Konsist documentation to learn more about Konsist and take a look at getting started guide.
Konsist API reflects the structure of Kotlin code. All declarations such as classes, functions, and properties can be queried and verified with the Konsist API. Take a look at a few examples below.
@Test
fun `classes with 'UseCase' suffix should reside in 'usecase' package`() {
Konsist.scopeFromProject()
.classes()
.withNameEndingWith("UseCase")
.assertTrue { it.resideInPackage("..usecase..") }
}
@Test
fun `classes extending 'ViewModel' should have 'ViewModel' suffix`() {
Konsist.scopeFromProject()
.classes()
.withAllParentsOf(ViewModel::class)
.assertTrue { it.name.endsWith("ViewModel") }
}
@Test
fun `interfaces with 'Repository' annotation should have 'Repository' suffix`() {
Konsist
.scopeFromProject()
.interfaces()
.withAllAnnotationsOf(Repository::class)
.assertTrue { it.hasNameEndingWith("Repository") }
}
@Test
fun `clean architecture layers have correct dependencies`() {
Konsist
.scopeFromProduction()
.assertArchitecture {
// Define layers
val domain = Layer("Domain", "com.myapp.domain..")
val presentation = Layer("Presentation", "com.myapp.presentation..")
val data = Layer("Data", "com.myapp.data..")
// Define architecture assertions
domain.dependsOnNothing()
presentation.dependsOn(domain)
data.dependsOn(domain)
}
}
Check out our snippet page for a feast of examples!
Read the Konsist articles to gain valuable insights into best practices and strategies for maintaining consistency in your projects.
Write a message on the #konsist channel at kotlinlang Slack Workspace (preferred) or start a GitHub discussion.
Please be sure to review Konsist contributing guidelines to learn how to support the project.
Konsist is distributed under the terms of the Apache License (Version 2.0). See LICENSE.md for details.