Kotlin Academy · Lesson

Assertions

Verify behavior.

Lesson 2 of 413 steps

Assertions is a free Kotlin Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.

What is an Assertion?

An assertion checks that an actual value matches what you expect. If it fails, the test fails with a helpful message.

JUnit5 provides assertions in org.junit.jupiter.api.Assertions.

assertEquals

The most common assertion. Note the order: expected first, actual second.

import org.junit.jupiter.api.Assertions.assertEquals

fun main() {
    val sum = 2 + 3
    assertEquals(5, sum)
    println("passed")
}

assertTrue and assertFalse

Assert boolean conditions directly.

import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Assertions.assertFalse

fun main() {
    assertTrue(10 > 5)
    assertFalse("".isNotEmpty())
    println("ok")
}

Null Assertions

Use assertNull and assertNotNull to verify nullability. assertNotNull also smart-casts in many cases.

import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Assertions.assertNull

fun main() {
    val name: String? = "Ada"
    assertNotNull(name)
    assertNull(null)
    println("done")
}

Asserting Exceptions

assertThrows verifies that a block throws a specific exception. It returns the exception so you can inspect its message.

import org.junit.jupiter.api.Assertions.assertThrows
import org.junit.jupiter.api.Assertions.assertEquals

fun main() {
    val ex = assertThrows(IllegalArgumentException::class.java) {
        require(false) { "bad input" }
    }
    assertEquals("bad input", ex.message)
    println("caught")
}

Grouped Assertions

assertAll runs multiple assertions and reports all failures together instead of stopping at the first.

import org.junit.jupiter.api.Assertions.assertAll
import org.junit.jupiter.api.Assertions.assertEquals

fun main() {
    assertAll(
        { assertEquals(2, 1 + 1) },
        { assertEquals(4, 2 + 2) }
    )
    println("all passed")
}

Failure Messages

Pass a message (or a lazy message lambda) to make failures self-explanatory.

import org.junit.jupiter.api.Assertions.assertEquals

fun main() {
    val balance = 100
    assertEquals(100, balance, "balance should be unchanged")
    println("ok")
}

Comparing Collections

assertEquals works for lists when order and contents match. Use assertIterableEquals for iterables of differing concrete types.

import org.junit.jupiter.api.Assertions.assertEquals

fun main() {
    val expected = listOf(1, 2, 3)
    val actual = (1..3).toList()
    assertEquals(expected, actual)
    println("lists equal")
}

Fluent Assertions with AssertK

Libraries like AssertK offer a readable assertThat(x).isEqualTo(y) style popular in Kotlin codebases.

// assertThat(result).isEqualTo(5)
// assertThat(list).contains("a", "b")
// assertThat(name).isNotNull()

fail()

Call fail to force a failure, useful for marking unreachable branches or unfinished tests.

import org.junit.jupiter.api.Assertions.fail

fun process(x: Int): String = when {
    x > 0 -> "positive"
    else -> fail("unexpected value")
}

Choosing the Right Assertion

Pick the most specific assertion for clear failures:

  • Equality → assertEquals
  • Conditions → assertTrue/assertFalse
  • Errors → assertThrows
  • Multiple checks → assertAll

Quick Check

You want a test to report every failed check at once rather than halting at the first. What do you use?

Recap

You now know JUnit5 assertions:

  • assertEquals (expected first), assertTrue/assertFalse
  • assertNull/assertNotNull, assertThrows
  • assertAll for grouped checks
  • Add messages for clarity

Next: mocking dependencies with MockK.

Free to start

Learn Kotlin with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Assertions” lesson free?

Yes — any 3 lessons of the Kotlin Academy learning path, including “Assertions”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.

What will I learn in “Assertions”?

Verify behavior. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Academy?

No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “Assertions” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Academy lesson?

Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Writing Tests with JUnit5
  2. Assertions
  3. Mocking with MockK
  4. Coroutine Testing
← Back to Kotlin Academy