Konsist can be used to guard the consistency of the Spring project.
@Test
fun `interfaces with 'Repository' annotation should have 'Repository' suffix`() {
Konsist
.scopeFromProject()
.interfaces()
.withAnnotationOf(Repository::class)
.assertTrue { it.hasNameEndingWith("Repository") }
}
@Test
fun `classes with 'RestController' annotation should have 'Controller' suffix`() {
Konsist
.scopeFromProject()
.classes()
.withAnnotationOf(RestController::class)
.assertTrue { it.hasNameEndingWith("Controller") }
}
@Test
fun `controllers never returns collection types`() {
/*
Avoid returning collection types directly. Structuring the response as
an object that contains a collection field is preferred. This approach
allows for future expansion (e.g., adding more properties like "totalPages")
without disrupting the existing API contract, which would happen if a JSON
array were returned directly.
*/
Konsist
.scopeFromPackage("story.controller..")
.classes()
.withAnnotationOf(RestController::class)
.functions()
.assertFalse { function ->
function.hasReturnType { it.isKotlinCollectionType }
}
}
@Test
fun `classes with 'RestController' annotation should reside in 'controller' package`() {
Konsist
.scopeFromProject()
.classes()
.withAnnotationOf(RestController::class)
.assertTrue { it.resideInPackage("..controller..") }
}
@Test
fun `classes with 'RestController' annotation should never return collection`() {
Konsist
.scopeFromPackage("story.controller..")
.classes()
.withAnnotationOf(RestController::class)
.functions()
.assertFalse { function ->
function.hasReturnType { it.hasNameStartingWith("List") }
}
}