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
  • 1. Classes Extending ViewModel Should Have ViewModel Suffix
  • 2. Every ViewModel Public Property Has Flow Type
  • 3. Repository Classes Should Reside In repository Package
  • 4. No Class Should Use Android Util Logging
  • 5. All JetPack Compose Previews Contain Preview In Method Name
  • 6. Every Class With Serializable Must Have Its Properties Serializable
Edit on GitHub
Export as PDF
  1. INSPIRATION
  2. Snippets

Android Snippets

PreviousGeneral SnippetsNextSpring Snippets

Last updated 5 months ago

Konsist can be used to guard the consistency of the project.

The project contains set of Konsist tests.

1. Classes Extending ViewModel Should Have ViewModel Suffix

@Test
fun `classes extending 'ViewModel' should have 'ViewModel' suffix`() {
    Konsist
        .scopeFromProject()
        .classes()
        .withParentClassOf(ViewModel::class)
        .assertTrue { it.name.endsWith("ViewModel") }
}

2. Every ViewModel Public Property Has Flow Type

@Test
fun `Every 'ViewModel' public property has 'Flow' type`() {
    Konsist
        .scopeFromProject()
        .classes()
        .withParentClassOf(ViewModel::class)
        .properties()
        .assertTrue {
            it.hasPublicOrDefaultModifier && it.hasType { type -> type.name == "kotlinx.coroutines.flow.Flow" }
        }
}

3. Repository Classes Should Reside In repository Package

@Test
fun `'Repository' classes should reside in 'repository' package`() {
    Konsist
        .scopeFromProject()
        .classes()
        .withNameEndingWith("Repository")
        .assertTrue { it.resideInPackage("..repository..") }
}

4. No Class Should Use Android Util Logging

@Test
fun `no class should use Android util logging`() {
    Konsist
        .scopeFromProject()
        .files
        .assertFalse { it.hasImport { import -> import.name == "android.util.Log" } }
}

5. All JetPack Compose Previews Contain Preview In Method Name

@Test
fun `All JetPack Compose previews contain 'Preview' in method name`() {
    Konsist
        .scopeFromProject()
        .functions()
        .withAnnotationOf(Preview::class)
        .assertTrue {
            it.hasNameContaining("Preview")
        }
}

6. Every Class With Serializable Must Have Its Properties Serializable

@Test
fun `every class with Serializable must have its properties Serializable`() {
    val message =
        """In Android, every serializable class must implement the Serializable interface 
    |or be a simple non-enum type because this is how the Java and Android serialization 
    |mechanisms identify which objects can be safely converted to a byte stream for 
    |storage or transmission, ensuring that complex objects can be properly reconstructed 
    |when deserialized.""".trimMargin()

    Konsist
        .scopeFromProduction()
        .classes()
        .withParentNamed("Serializable")
        .properties()
        .types
        .sourceDeclarations()
        .withoutKotlinBasicTypeDeclaration()
        .withoutClassDeclaration { it.hasEnumModifier }
        .assertTrue(additionalMessage = message) {
            it.asClassDeclaration()?.hasParentWithName("Serializable")
        }
}
🔍
Android
android-showcase