> 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/clean-architecture-test-snippets.md).

# Clean Architecture Test Snippets

Snippets used to guard clean architecture dependencies.

## 1. Clean Architecture Layers Have Correct Dependencies

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

## 2. Classes With `UseCase` Suffix Should Reside In `domain` And `usecase` Package

```kotlin
@Test
fun `classes with 'UseCase' suffix should reside in 'domain' and 'usecase' package`() {
    Konsist
        .scopeFromProject()
        .classes()
        .withNameEndingWith("UseCase")
        .assertTrue { it.resideInPackage("..domain..usecase..") }
}
```

## 3. Classes With `UseCase` Suffix Should Have Single `public Operator` Method Named `invoke`

```kotlin
@Test
fun `classes with 'UseCase' suffix should have single 'public operator' method named 'invoke'`() {
    Konsist
        .scopeFromProject()
        .classes()
        .withNameEndingWith("UseCase")
        .assertTrue {
            val hasSingleInvokeOperatorMethod = it.hasFunction { function ->
                function.name == "invoke" && function.hasPublicOrDefaultModifier && function.hasOperatorModifier
            }

            hasSingleInvokeOperatorMethod && it.countFunctions { item -> item.hasPublicOrDefaultModifier } == 1
        }
}
```

## 4. Classes With `UseCase` Suffix And Parents Should Have Single `public Operator` Method Named `invoke`

```kotlin
@Test
fun `classes with 'UseCase' suffix and parents should have single 'public operator' method named 'invoke'`() {
    Konsist
        .scopeFromProject()
        .classes()
        .withNameEndingWith("UseCase")
        .assertTrue {
            // Class and its parents
            val declarations = listOf(it) + it.parents(indirectParents = true)
                .mapNotNull { parent -> parent.sourceDeclaration?.asClassOrInterfaceDeclaration() }

            // Functions from all parents without overrides
            val uniqueFunctions = declarations
                .mapNotNull { koParentDeclaration -> koParentDeclaration as? KoFunctionProvider }
                .flatMap { koFunctionProvider ->
                    koFunctionProvider.functions(
                        includeNested = false,
                        includeLocal = false
                    )
                }
                .filterNot { koFunctionDeclaration -> koFunctionDeclaration.hasOverrideModifier }

            val hasInvokeOperatorMethod = uniqueFunctions.any { functionDeclaration ->
                functionDeclaration.name == "invoke" && functionDeclaration.hasPublicOrDefaultModifier && functionDeclaration.hasOperatorModifier
            }

            val numParentPublicFunctions = uniqueFunctions.count { functionDeclaration ->
                functionDeclaration.hasPublicOrDefaultModifier
            }

            hasInvokeOperatorMethod && numParentPublicFunctions == 1
        }
}
```

## 5. Interfaces With `Repository` Annotation Should Reside In `data` Package

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

## 6. Every UseCase Class Has Test

```kotlin
@Test
fun `every UseCase class has test`() {
    Konsist
        .scopeFromProduction()
        .classes()
        .withNameEndingWith("UseCase")
        .assertTrue { it.hasTestClasses() }
}
```


---

# 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/clean-architecture-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.
