Kotlin Tutorial #25: Testing in Kotlin — JUnit 5, MockK, and Best Practices

In the previous tutorial, you built a REST API with Ktor. Now let’s learn about testing. Good tests make your code reliable and give you confidence to refactor. In this tutorial, you will learn how to write tests in Kotlin using JUnit 5 and MockK. In this tutorial, you will learn: JUnit 5 basics (@Test, @BeforeEach, @Nested) Assertions (assertEquals, assertTrue, assertThrows) Parameterized tests (@ParameterizedTest, @ValueSource, @CsvSource) MockK (mock, every, verify) Argument capture and verification order Testing coroutines with runTest Test organization and best practices Setting Up Add MockK and coroutines-test to your build.gradle.kts: ...

March 22, 2026 · 8 min

Kotlin Tutorial #24: Build a REST API with Ktor

In the previous tutorial, you built a CLI tool. Now let’s build a REST API. Ktor is Kotlin’s official framework for building asynchronous servers and clients. It is lightweight, flexible, and uses Kotlin coroutines. In this tutorial, you will learn: Setting up Ktor Routing (GET, POST, PUT, DELETE) JSON serialization with Content Negotiation In-memory storage Path parameters and query parameters Error handling with StatusPages Testing with ktor-server-test-host What We’re Building A Notes API with CRUD operations: ...

March 22, 2026 · 9 min

Kotlin Tutorial #23: Build a CLI Tool with Kotlin

In the previous tutorial, you learned about Kotlin Serialization. Now let’s build something real. In this tutorial, you will build a complete CLI (Command-Line Interface) tool: a text analyzer that counts words, finds patterns, and shows statistics. In this tutorial, you will learn: Reading command-line arguments File I/O (reading files) Text analysis (word count, frequency) Colored terminal output with ANSI codes Argument parsing Error handling Structuring a real project What We’re Building A text analysis tool called TextAnalyzer with these commands: ...

March 22, 2026 · 8 min

Kotlin Tutorial #22: Kotlin Serialization and Working with JSON

In the previous tutorial, you learned about Kotlin DSLs. Now let’s learn about Kotlin Serialization. Serialization is the process of converting objects to a format like JSON, and deserialization is the reverse. Kotlin Serialization is the official library for this. In this tutorial, you will learn: Setting up Kotlin Serialization @Serializable and basic encoding/decoding Default values and optional fields @Transient for ignoring fields @SerialName for custom field names Nested objects and collections Enum serialization Json configuration options Polymorphic serialization JsonElement API for raw JSON Practical examples Setting Up Add the serialization plugin and dependency to your build.gradle.kts: ...

March 22, 2026 · 8 min

Kotlin Tutorial #21: Kotlin DSLs — Writing Expressive APIs

In the previous tutorial, you learned about inline functions and reified types. Now let’s learn about DSLs. A DSL (Domain-Specific Language) is a small language designed for a specific task. Kotlin makes it easy to create DSLs using lambdas with receivers. In this tutorial, you will learn: Lambdas with receiver Building a config DSL Nested DSL builders @DslMarker annotation HTML builder DSL Route DSL Query builder DSL Gradle-style DSL What is a DSL? A DSL is a language designed for a specific domain. You already use Kotlin DSLs every day: ...

March 22, 2026 · 9 min

Kotlin Tutorial #20: Inline Functions, Reified Types, and Contracts

In the previous tutorial, you learned about Flow. Now let’s learn about inline functions, reified types, and contracts. These are advanced Kotlin features that help you write faster code, work with generic types at runtime, and give the compiler extra information about your functions. In this tutorial, you will learn: Inline functions and why they matter Non-local returns noinline and crossinline Reified type parameters Contracts (callsInPlace, returns) Practical examples What Are Inline Functions? When you pass a lambda to a function, Kotlin creates a lambda object behind the scenes. This has a small performance cost: memory allocation and method calls. ...

March 22, 2026 · 9 min

Kotlin Tutorial #19: Flow — Reactive Streams in Kotlin

In the previous tutorial, you learned about advanced coroutine topics. Now let’s learn about Flow. Flow is Kotlin’s way of working with reactive streams. It lets you emit multiple values over time, with full support for coroutines. In this tutorial, you will learn: What is Flow Flow builder and emit/collect Operators: map, filter, take, transform Terminal operators: toList, first, reduce, fold flowOn for changing dispatchers combine and zip Error handling with catch and retry onCompletion and onEach conflate and distinctUntilChanged flatMapConcat, flatMapMerge, flatMapLatest StateFlow and SharedFlow (hot flows) stateIn and shareIn Practical examples What is Flow? Flow is a cold, asynchronous stream that emits values one at a time. ...

March 22, 2026 · 12 min

Kotlin Tutorial #18: Coroutines Deep Dive — Error Handling, SupervisorJob, and Channels

In the previous tutorial, you learned the basics of coroutines: suspend functions, launch, async, and structured concurrency. Now let’s dive deeper into error handling, supervisor jobs, and channels. In this tutorial, you will learn: CoroutineExceptionHandler SupervisorJob and supervisorScope Channels (send, receive, close) Channel types (buffered, conflated, unlimited) produce and consumeEach Fan-out and fan-in patterns select expression Mutex for shared mutable state withTimeout and withTimeoutOrNull Practical patterns Error Handling in Coroutines Error handling in coroutines is different from regular code. When a coroutine throws an exception, the behavior depends on whether you use launch or async. ...

March 22, 2026 · 10 min

Kotlin Tutorial #16: Sequences — Lazy Collections for Performance

In the previous tutorial, you learned about delegation. Now let’s learn about sequences. Sequences are lazy collections that process elements one at a time instead of all at once. This can make a big difference in performance when working with large data sets. In this tutorial, you will learn: Sequences vs Lists (lazy vs eager) Creating sequences generateSequence Sequence builders with yield and yieldAll Infinite sequences When to use sequences Performance comparison Practical examples Sequences vs Lists Lists are eager — they process all elements at each step and create intermediate collections. Sequences are lazy — they process one element through all steps before moving to the next. ...

March 22, 2026 · 9 min

Kotlin Tutorial #15: Delegation — by lazy, Delegates, and Custom Delegation

In the previous tutorial, you learned about error handling. Now let’s learn about delegation. Delegation is a design pattern where an object hands off work to another object. Kotlin has built-in support for delegation using the by keyword. In this tutorial, you will learn: by lazy — lazy initialization Delegates.observable — react to changes Delegates.vetoable — reject invalid changes Map delegation — properties from a map Custom delegates — build your own Class delegation — delegate interface implementation by lazy by lazy creates a property that is computed only once, the first time you access it. After that, the cached value is returned. This is useful for expensive initialization. ...

March 22, 2026 · 8 min