androidengineers.Book a session

Testing in Kotlin

Practice: Comprehensive Test Suite

exercise50 minHard

Verify a planner across layers

Build a suite around a planner that parses a request, validates a positive duration, stores one session, and returns a confirmation. Separate pure parsing tests from repository interaction tests and a small integration test with an in-memory implementation.

interface Sessions { fun save(topic: String, minutes: Int) }

fun schedule(topic: String, minutes: Int, sessions: Sessions): String {
    require(topic.isNotBlank() && minutes > 0)
    sessions.save(topic.trim(), minutes)
    return "Scheduled"
}

Use a recording fake to assert that valid input saves exactly one normalized session. Invalid input should fail before any save. Define what happens when persistence throws; do not claim success if the write did not complete.

Acceptance checks

Cover valid data, whitespace, blank topic, zero and negative duration, repository failure, and independent repeated invocations. Confirm tests do not share a mutable fake instance across methods.

Extension: make persistence suspending and migrate lifetime tests to runTest. Keep pure validation tests synchronous. Record which behaviors require an integration test rather than increasing mocks until every detail appears covered.

Reference: Testing Kotlin/JVM

YOUR LEARNING JOURNEY

0 of 110 available lessons completed

Progress saved in this browser. No account needed.
Practice: Comprehensive Test Suite | Kotlin Core Programming | Android Engineers