For the complete documentation index, see llms.txt. This page is also available as Markdown.

Declaration Assertion

Verify codebase using Konsist API

Assertions are used to perform code base verification. This is the final step of Konsist verification preceded by scope creation (Create The Scope) and Declaration Filtering steps:

Assertion Methods

Konsist offers a variety of assertion methods. These can be applied to a list of KoDeclarations as well as a single declaration.

Assert True

In the below snippet, the assertion (performed on the list of interfaces) verifies if every interface has a public visibility modifier.

koScope
    .interfaces()
    .assertTrue { it.hasPublicModifier }

The it parameter inside the assertTrue method represents a single declaration (single interface in this case). However, the assertion itself will be performed on every available interface. The last line in the assertTrue block will be evaluated as true or false providing the result for a given declaration.

Each KoDeclaration comes with an API, comprising methods and properties, for verifying the declaration. Additionally, the Konsist API offers a text property for exceptional cases where the standard API falls short. This should be used as a last resort, and any issues encountered should be reported (see Getting Help).

Assert False

The assertFalse is a negation of the assertTrue method. In the below snippet, the assertion (performed on the list of properties) verifies if none of the properties has the Inject annotation:

This assertion verifies that no property has public (an explicit public modifier) or default (implicit public modifier) modifiers:

Assert Empty

This assertion helps to verify if the given list of declarations is empty.

Assert Not Empty

This assertion helps to verify if the given list of declarations is not empty.

Assertion Parameters

Test Name

Assertions offer a set of parameters allowing you to tweak the assertion behavior. By default, the test name is derived from the JUnit test method name (for JUnit 5 dynamic tests, the name of the @TestFactory method). For Kotest the test name can't be determined, so it is omitted from the error message. Register KoJUnitExtension (JUnit 5 dynamic tests) or KoKotestExtension (Kotest 6) to use the name of the running test (see Explicit Test Names), or set the testName argument to use a custom test name (displayed in the error message and used for suppression, see Suppress Konsist Test).

Strict

You can also enable strict verification (the assertion fails if the declaration list is empty or contains only null values) by setting the strict argument to true:

Additional Message

The additionalMessage param allows providing an additional message that will be displayed with the failing test. This may be a more detailed description of the problem or a hint on how to fix the issue.

Rule

The rule param accepts a KoRule describing what the assertion checks, why the rule exists and how to fix a violation. The rule is displayed in the failure message, so a developer (or an AI agent) knows how to fix the issue:

Field
Required
Label in message
Description

id

Yes

ID:

Unique rule identifier (e.g. VM-001), so a developer or an AI agent can refer to the rule.

title

Yes

RULE:

One sentence that says what the rule checks.

description

No

DESCRIPTION:

Additional details about the rule, e.g. its scope or exceptions.

reason

Yes

REASON:

Why the rule exists.

howToFix

No

HOW TO FIX:

How to fix a violation.

goodExample

No

GOOD EXAMPLE:

A short code sample that follows the rule.

badExample

No

BAD EXAMPLE:

A short code sample that violates the rule.

  • Fields are displayed in the order listed above.

  • Required fields must not be blank (otherwise IllegalArgumentException is thrown). Blank optional fields are not displayed.

  • Common indentation is removed from goodExample and badExample, so a raw string ("""...""") can be used.

  • When both rule and additionalMessage are set, the rule is displayed first.

The rule param is available for every assertion (assertTrue, assertFalse, assertEmpty, assertNotEmpty, assertNull, assertNotNull) and for assertArchitecture (see Architecture Assertion).

Add Failure Reason

Use addFailureReason inside the assertTrue / assertFalse block to explain why a given declaration violates the assertion. Reasons are displayed under the failing declaration. Reasons added for declarations that pass the assertion are ignored:

additionalMessage is displayed once for the whole assertion, while addFailureReason is displayed for each failing declaration. addFailureReason can be called multiple times for a single declaration.

Gradle does not display the assertion message (including additionalMessage, rule and failure reasons) in the command line output by default. See Enable Full Command Line Logging.

Last updated