0Pricing
Kotlin Academy · Lesson

Assertions

Verify behavior.

Assertions is a free Kotlin Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “Assertions” lesson free?

Yes — the full text of “Assertions” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

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