Konsist
GitHubSlack (kotlinlang)Twitter
  • 🚀GETTING STARTED
    • What is Konsist?
    • Getting Started
      • Add Konsist Dependency
      • Create First Konsist Test - Declaration Check
      • Create Secound Konsist Test - Architectural Check
    • Articles & Videos
  • ✅WRITING TESTS
    • Create The Scope
    • Declaration Filtering
    • Declaration Assertion
    • Architecture Assertion
    • Suppress Konsist Test
  • ✏️VERYFYING CODEBASE
    • Verify Classes
    • Verify Interfaces
    • Verify Functions
    • Verify Properties
    • Verify Generics
    • Verify Source Declarations
  • 📗FEATURES
    • Add Konsist Existing To Project (Baseline)
    • Debug Konsist Test
    • Declaration
    • Declaration Vs Property
    • Compiler Type Inference
    • Package Wildcard
    • Declaration References
    • Indirect Parents
    • Kotest Support
  • 🔍INSPIRATION
    • Starter Projects
    • Snippets
      • General Snippets
      • Android Snippets
      • Spring Snippets
      • Test Snippets
      • JUnit Snippets
      • Kotest Snippets
      • Architecture Snippets
      • Clean Architecture Snippets
      • Kotlin Serialization Snippets
      • Library Snippets
      • Generic Types Snippets
  • 🎓ADVANCED
    • Isolate Konsist Tests
    • Enable Full Command Line Logging
    • Dynamic Konsist Tests
      • Explicit Test Names
    • When Konsist API Is Not Enough
    • Additional JUnit5 Setup
    • Why There Are No Pre-defined Rules?
    • Konsist Snapshots
  • ❓HELP
    • Getting Help
    • Known Issues
      • java.lang.OutOfMemoryError: Java heap space
    • Compatibility
  • ℹ️OTHER
    • Changelog
    • Project Status
    • Contributing
    • Contributors
    • Assets And Logos
    • Open Source Licenses
    • Sponsor Konsist
Powered by GitBook
On this page
  • Assertion Methods
  • Assert True
  • Assert False
  • Assert Empty
  • Assert Not Empty
  • Assertion Parameters
  • Test Name
  • Strict
  • Additional Message
Edit on GitHub
Export as PDF
  1. WRITING TESTS

Declaration Assertion

Verify codebase using Konsist API

PreviousDeclaration FilteringNextArchitecture Assertion

Last updated 3 months ago

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 asset.

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 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:

Konist
    .scopeFromProject()
    .properties()
    .assertFalse { 
        it.hasAnnotationOf(Inject::class)
    }

This assertion verifies that the class does not contain any properties with public (an explicit public modifier) or default (implicit public modifier) modifiers:

Konist
    .scopeFromProject()
    .properties()
    .assertFalse { 
        it.hasPublicOrDefaultModifier
    }

Assert Empty

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

Konist
    .scopeFromProject()
    .classes()
    .assertEmpty()

Assert Not Empty

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

Konist
    .scopeFromProject()
    .classes()
    .assertNotEmpty()

Assertion Parameters

Test Name

Assertions offer a set of parameters allowing to tweak the assertion behavior. You can adjust several settings, such as setting testName that helps with suppression (see Suppress Konsist Test).

Strict

You can also enable enhanced verification by setting strict argument to true:

Konist
    .scopeFromProject() 
    .classes()
    .assertFalse(strict = true) { ... }

Additional Message

The additionalMessage param allows to provision of additional messages 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.

Konist
    .scopeFromProject() 
    .classes()
    .assertFalse(additionalMessage = "Do X to fix the issue") { ... }
✅