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 #17: Coroutines — launch, async, and Structured Concurrency

In the previous tutorial, you learned about sequences. Now let’s learn about coroutines. Coroutines are Kotlin’s way of writing asynchronous code. They let you write code that looks synchronous but runs without blocking threads. In this tutorial, you will learn: Suspend functions launch — fire and forget async/await — get a result Dispatchers coroutineScope Structured concurrency Jobs and cancellation Timeouts Practical examples What Are Coroutines? A coroutine is a lightweight thread. You can run thousands of coroutines on a single thread. Unlike threads, coroutines are cheap to create and do not block the operating system thread they run on. ...

March 19, 2026 · 9 min