> 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/getting-started/getting-started/create-secound-konsist-test-architectural-check.md).

# Create Second Konsist Test - Architectural Check

Konsist's `Architectural Checks` serve as a robust tool for maintaining layer isolation, enabling development teams to enforce strict boundaries between different architectural layers. Here are a few things that can be verified with Konsist:

* `domain` layer is independent
* `data` layer depends on `domain` layer
* ...

{% hint style="info" %}
See [Architecture Test Snippets](/samples/starter-test-snippets/architecture-test-snippets.md) section for more examples.
{% endhint %}

## Write First Architectural Check

Let's write a simple test to verify that application architecture rules are preserved. In this scenario, the application follows a simple 3-layer architecture, where `Presentation` and `Data` layers depend on `Domain` layer and `Domain` layer is independent (from these layers):

```mermaid
%%{init: {'theme':'forest'}}%%
flowchart TD
    Presentation["Presentation Layer"]-->Domain["Domain Layer"]
    Data["Data Layer"]-->Domain
```

### Overview

On a high level writing Konsist `architectural check` requires 3 steps:

```mermaid
%%{init: {'theme':'forest'}}%%
flowchart TB
    Step1["1\. Define Layers"]-->Step2
    Step2["2\. Create The Scope"]-->Step3
    Step3["3\. Assert Architecture"]
```

Let's take a closer look at each of these steps.

### 1. Define Layers

Create layer instances to represent project layers. Each `Layer` instance accepts the `name` (used for presenting architecture violation errors) and `rootPackage` used to define the layer.

```kotlin
// Define layers
private val presentationLayer = Layer("Presentation", "com.myapp.presentation..")
private val domainLayer = Layer("Domain", "com.myapp.domain..")
private val dataLayer = Layer("Data", "com.myapp.data..")
```

{% hint style="info" %}
The double dot syntax (`..`) means zero or more packages - layer is represented by the package and all of its subpackages (see [Package Wildcard](/features/packageselector.md) syntax).
{% endhint %}

### 2. Create The Scope

The `Konsist` object is an entry point to the `Konsist` library.

```kotlin
Konsist
```

The `scopeFromX` methods obtain the instance of the scope containing Kotlin project files. To get all Kotlin project files present in the project use the `scopeFromProject` method:

```kotlin
// Define layers
private val presentationLayer = Layer("Presentation", "com.myapp.presentation..")
private val domainLayer = Layer("Domain", "com.myapp.domain..")
private val dataLayer = Layer("Data", "com.myapp.data..")
 
// Define the scope containing all Kotlin files present in the project
Konsist.scopeFromProject() //Returns KoScope
```

{% hint style="info" %}
To define more granular scopes such as scope from production code or scope from single module see the [Create The Scope](/writing-tests/koscope.md) page.
{% endhint %}

### 3. Assert Architecture

To perform an assertion use the `assertArchitecture` method:

<pre class="language-kotlin"><code class="lang-kotlin">// Define layers
private val presentationLayer = Layer("Presentation", "com.myapp.presentation..")
private val domainLayer = Layer("Domain", "com.myapp.domain..")
private val dataLayer = Layer("Data", "com.myapp.data..")

Konsist
    .scopeFromProject()
     // Assert architecture
    .assertArchitecture {
<strong>        // Define architectural rules
</strong>    }
</code></pre>

Utilize `dependsOn` and `dependsOnNothing` methods to validate that your project's layers adhere to the defined architectural dependencies:

```kotlin
Konsist
    .scopeFromProject()
    .assertArchitecture {
        val presentationLayer = Layer("Presentation", "com.myapp.presentation..")
        val domainLayer = Layer("Domain", "com.myapp.domain..")
        val dataLayer = Layer("Data", "com.myapp.data..")

        // Define layer dependencies
        presentationLayer.dependsOn(domainLayer)
        dataLayer.dependsOn(domainLayer)
        domainLayer.dependsOnNothing()
    }
```

## Wrap Konsist Code In Test

The architecture validation logic should be protected through automated testing. By wrapping Konsist checks within standard testing frameworks such as [JUnit](https://junit.org) or [Kotest](https://kotest.io/), you can verify these rules with each [Pull Request](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests):

{% tabs %}
{% tab title="JUnit" %}

```kotlin
class ArchitectureKonsistTest {
    @Test
    fun `architecture layers have correct dependencies`() {
        Konsist
            .scopeFromProject()
            .assertArchitecture {
                val presentationLayer = Layer("Presentation", "com.myapp.presentation..")
                val domainLayer = Layer("Domain", "com.myapp.domain..")
                val dataLayer = Layer("Data", "com.myapp.data..")
        
                // Define layer dependencies
                presentationLayer.dependsOn(domainLayer)
                dataLayer.dependsOn(domainLayer)
                domainLayer.dependsOnNothing()
            }
    }
}
```

{% hint style="info" %}
The [JUnit](https://junit.org/) testing framework project dependency should be added to the project. See [starter projects](https://github.com/LemonAppDev/konsist/tree/main/samples/starter-projects) to get a complete sample project.
{% endhint %}
{% endtab %}

{% tab title="Kotest" %}

```kotlin
class ArchitectureKonsistTest : FreeSpec({
    "architecture layers have correct dependencies" {
        Konsist
            .scopeFromProject()
            .assertArchitecture(testName = this.testCase.name.name) {
                val presentationLayer = Layer("Presentation", "com.myapp.presentation..")
                val domainLayer = Layer("Domain", "com.myapp.domain..")
                val dataLayer = Layer("Data", "com.myapp.data..")

                // Define layer dependencies
                presentationLayer.dependsOn(domainLayer)
                dataLayer.dependsOn(domainLayer)
                domainLayer.dependsOnNothing()
            }
    }
})
```

{% hint style="info" %}
For Kotest to function correctly the Kotest test name has to be explicitly passed. See the [Kotest Support](/features/kotest-support.md) page.
{% endhint %}

{% hint style="info" %}
The [Kotest](https://kotest.io/) testing framework project dependency should be added to the project. See [starter projects](https://github.com/LemonAppDev/konsist/tree/main/samples/starter-projects) to get a complete sample project.
{% endhint %}
{% endtab %}
{% endtabs %}

Note that the test class has a `KonsistTest` suffix. This is the recommended approach to name classes containing Konsist tests.

## Summary

This section described the basic way of writing a Konsist architectural test. To get a better understanding of how Konsist API works see [Debug Konsist Test](/features/debug-konsist-test.md).


---

# 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/getting-started/getting-started/create-secound-konsist-test-architectural-check.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.
