Introduction to Konsist
Konsist is an innovative linter designed to ensure the consistency and cohesiveness of projects developed in Kotlin. It operates by enforcing a unified code structure and architecture. The magic happens through unit tests written using popular frameworks like JUnit or Kotest.
How to Incorporate Konsist
Depending on your build tool, adding Konsist to your project is straightforward. Here are the options:
-
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>
For more detailed guidance on setting things up, check out the Konsist documentation.
Practical Examples
Konsist offers a versatile API that aligns with Kotlin's code structure, allowing developers to query and verify elements like classes, functions, and properties. Here are some examples that demonstrate Konsist's functionality:
General Kotlin Check
This test ensures that classes with the suffix "UseCase" are located in the "usecase" package:
@Test
fun `classes with 'UseCase' suffix should reside in 'usecase' package`() {
Konsist.scopeFromProject()
.classes()
.withNameEndingWith("UseCase")
.assertTrue { it.resideInPackage("..usecase..") }
}
Android Specific Check
This test verifies that classes extending "ViewModel" have the appropriate "ViewModel" suffix:
@Test
fun `classes extending 'ViewModel' should have 'ViewModel' suffix`() {
Konsist.scopeFromProject()
.classes()
.withAllParentsOf(ViewModel::class)
.assertTrue { it.name.endsWith("ViewModel") }
}
Spring Specific Check
Konsist can enforce naming conventions for interfaces with specific annotations:
@Test
fun `interfaces with 'Repository' annotation should have 'Repository' suffix`() {
Konsist
.scopeFromProject()
.interfaces()
.withAllAnnotationsOf(Repository::class)
.assertTrue { it.hasNameEndingWith("Repository") }
}
Architecture Layers Check
Ensure that the different layers of your architecture adhere to your dependency rules:
@Test
fun `clean architecture layers have correct dependencies`() {
Konsist
.scopeFromProduction()
.assertArchitecture {
val domain = Layer("Domain", "com.myapp.domain..")
val presentation = Layer("Presentation", "com.myapp.presentation..")
val data = Layer("Data", "com.myapp.data..")
domain.dependsOnNothing()
presentation.dependsOn(domain)
data.dependsOn(domain)
}
}
For more examples, visit the snippet page.
Additional Resources
To dive deeper into Konsist's best practices and strategies, explore the Konsist articles.
Join the Community
Join the conversation on the #konsist channel in the Kotlinlang Slack Workspace or participate in a GitHub discussion.
Contributing to Konsist
Interested developers are encouraged to read the Konsist contributing guidelines for information on how to support and contribute to the project.
Licensing
Konsist is available under the Apache License (Version 2.0). Detailed license information can be found in the LICENSE.md file.