Test assertions help you check if your code works as expected by comparing actual results with what you want.
Kotlin test assertions
assertEquals(expected, actual)
assertTrue(condition)
assertFalse(condition)
assertNotNull(value)
assertNull(value)
assertFailsWith<ExceptionType> { code }These functions come from Kotlin's test libraries like kotlin.test or JUnit.
You usually write these inside test functions to check your code automatically.
assertEquals(5, sum(2, 3))
isValid is true.assertTrue(isValid)
parseNumber with "abc" throws an IllegalArgumentException.assertFailsWith<IllegalArgumentException> {
parseNumber("abc")
}This program tests the add function with different assertions. It also checks that converting "abc" to an integer throws a NumberFormatException.
import kotlin.test.* fun add(a: Int, b: Int): Int = a + b fun main() { assertEquals(7, add(3, 4)) assertTrue(add(1, 1) == 2) assertFalse(add(2, 2) == 5) assertNotNull(add(0, 0)) assertNull(null) val exception = assertFailsWith<NumberFormatException> { "abc".toInt() } println("Caught exception: ${exception.message}") }
Use clear messages in assertions to understand failures better.
Assertions help catch bugs early by testing small parts of your code.
Run tests often to keep your code reliable.
Assertions check if your code behaves as expected.
Use different assertions for equality, truth, null checks, and exceptions.
Write assertions inside test functions to automate checking your code.
Practice
Solution
Step 1: Understand assertion types
assertEqualsis used to check if two values are equal.Step 2: Match assertion to purpose
assertTruechecks if a condition is true,assertNullchecks for null, andassertFailschecks for exceptions.Final Answer:
assertEquals(expected, actual) -> Option AQuick Check:
Equality check = assertEquals [OK]
- Confusing assertTrue with assertEquals
- Using assertNull for equality checks
- Using assertFails to check values
Solution
Step 1: Recall Kotlin assertion syntax
assertTruetakes a Boolean condition as a parameter inside parentheses.Step 2: Identify correct syntax
OnlyassertTrue(condition)is valid Kotlin syntax; others are incorrect.Final Answer:
assertTrue(condition) -> Option CQuick Check:
assertTrue uses parentheses for condition [OK]
- Using curly braces instead of parentheses
- Omitting parentheses
- Using colon instead of parentheses
val result = 5 assertEquals(5, result)
Solution
Step 1: Check values compared
The expected value is 5 and actual value stored inresultis also 5.Step 2: Understand assertEquals behavior
Since both values are equal,assertEqualspasses without throwing an error.Final Answer:
Test passes without error -> Option AQuick Check:
Equal values = test passes [OK]
- Assuming assertEquals throws error on equal values
- Confusing expected and actual order
- Thinking assertEquals returns a value
assertEquals(10, 5 + 3)
Solution
Step 1: Calculate actual value
The expression5 + 3equals 8.Step 2: Compare expected and actual
Expected is 10 but actual is 8, soassertEqualsfails and throws AssertionError.Final Answer:
Test fails because expected and actual differ -> Option DQuick Check:
Different values = test fails [OK]
- Assuming test passes despite value mismatch
- Thinking syntax error occurs
- Confusing runtime exceptions with assertion failures
Solution
Step 1: Understand exception testing
To check if a function throws an exception, useassertFailswith a lambda calling the function.Step 2: Evaluate other options
assertEqualscompares values, not exceptions;assertNullchecks for null;assertTruechecks a Boolean condition.Final Answer:
assertFails { function(invalidInput) } -> Option BQuick Check:
Exception test = assertFails [OK]
- Using assertEquals to check exceptions
- Using assertNull expecting exception
- Using assertTrue for exception testing
Ready to prove it?
Pass the test and this pattern moves unknown
