> For the complete documentation index, see [llms.txt](https://docs.konsist.lemonappdev.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.konsist.lemonappdev.com/samples/starter-test-snippets/spring-test-snippets.md).

# Spring Test Snippets

Konsist can be used to guard the consistency of the [Spring](https://spring.io/) project.

## 1. Interfaces With `Repository` Annotation Should Have `Repository` Suffix

```kotlin
@Test
fun `interfaces with 'Repository' annotation should have 'Repository' suffix`() {
    Konsist
        .scopeFromProject()
        .interfaces()
        .withAnnotationOf(Repository::class)
        .assertTrue { it.hasNameEndingWith("Repository") }
}
```

## 2. Classes With `RestController` Annotation Should Have `Controller` Suffix

```kotlin
@Test
fun `classes with 'RestController' annotation should have 'Controller' suffix`() {
    Konsist
        .scopeFromProject()
        .classes()
        .withAnnotationOf(RestController::class)
        .assertTrue { it.hasNameEndingWith("Controller") }
}
```

## 3. Controllers Never Return Collection Types

```kotlin
@Test
fun `controllers never return 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.sourceDeclaration?.isKotlinCollectionType == true }
        }
}
```

## 4. Classes With `RestController` Annotation Should Reside In `controller` Package

```kotlin
@Test
fun `classes with 'RestController' annotation should reside in 'controller' package`() {
    Konsist
        .scopeFromProject()
        .classes()
        .withAnnotationOf(RestController::class)
        .assertTrue { it.resideInPackage("..controller..") }
}
```

## 5. Classes With `RestController` Annotation Should Never Return Collection

```kotlin
@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") }
        }
}
```

## 6. Service Classes Should Be Annotated With Service Annotation

```kotlin
@Test
fun `Service classes should be annotated with Service annotation`() {
    Konsist
        .scopeFromProject()
        .classes()
        .withNameEndingWith("Service")
        .assertTrue { it.hasAnnotationOf(Service::class) }
}
```

## 7. Entity Classes Should Have An Id Field

```kotlin
@Test
fun `Entity classes should have an Id field`() {
    Konsist
        .scopeFromProject()
        .classes()
        .withAnnotationOf(Entity::class)
        .assertTrue { clazz ->
            clazz.properties().any { property ->
                property.hasAnnotationOf(Id::class)
            }
        }
}
```

## 8. DTO Classes Should Be Data Classes

```kotlin
@Test
fun `DTO classes should be data classes`() {
    Konsist
        .scopeFromProject()
        .classes()
        .withNameEndingWith("DTO")
        .assertTrue { it.hasModifier(KoModifier.DATA) }
}
```

## 9. RestControllers Should Not Have State Fields

```kotlin
@Test
fun `RestControllers should not have state fields`() {
    Konsist
        .scopeFromProject()
        .classes()
        .withAnnotationOf(RestController::class)
        .objects()
        .withModifier(KoModifier.COMPANION)
        .assertTrue {
            it.properties().isEmpty()
        }
}
```

## 10. Files With Domain Package Do Not Have Spring References

```kotlin
@Test
fun `files with domain package do not have Spring references`() {
    Konsist.scopeFromProduction()
        .files
        .withPackage("..domain..")
        .assertFalse {
            it
                .imports
                .any { import ->
                    import.name.startsWith("org.springframework")
                }
        }
}
```

## 11. Transactional Annotation Should Only Be Used On Default Or Public Methods That Are Not Part Of An Interface

```kotlin
@Test
fun `Transactional annotation should only be used on default or public methods that are not part of an interface`() {
    Konsist.scopeFromProject()
        .functions()
        .withAnnotationOf(Transactional::class)
        .assertTrue {
            it.hasPublicOrDefaultModifier && it.containingDeclaration !is KoInterfaceDeclaration
        }
}
```

## 12. Every API Method In RestController With `Admin` Suffix Should Have PreAuthorize Annotation With ROLE\_ADMIN

```kotlin
@Test
fun `every API method in RestController with 'Admin' suffix should have PreAuthorize annotation with ROLE_ADMIN`() {
    Konsist.scopeFromProject()
        .classes()
        .withAnnotationOf(RestController::class)
        .withNameEndingWith("Admin")
        .functions()
        .assertTrue {
            it.hasAnnotationOf(PreAuthorize::class) && it.text.contains("hasRole('ROLE_ADMIN')")
        }
}
```

## 13. Every Non-public Controller Should Have @PreAuthorize On Class Or On Each Endpoint Method

```kotlin
@Test
fun `every non-public Controller should have @PreAuthorize on class or on each endpoint method`() {
    Konsist.scopeFromProject()
        .classes()
        .withAnnotationOf(RestController::class)
        .filterNot { it.hasPublicOrDefaultModifier }
        .assertTrue { controller ->
            controller.hasAnnotationOf(PreAuthorize::class) ||
                    controller.functions()
                        .all { it.hasAnnotationOf(PreAuthorize::class) }
        }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.konsist.lemonappdev.com/samples/starter-test-snippets/spring-test-snippets.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
