Writing Tests with JUnit5
Structure tests.
Writing Tests with JUnit5 is a free Kotlin Academy lesson on CoddyKit — lesson 1 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 JUnit5?
JUnit5 (JUnit Jupiter) is the standard testing framework for the JVM and Kotlin. It provides annotations to mark and structure test code.
- Tests live in
src/test/kotlin - Each test is a function
- The runner executes them and reports results
Adding the Dependency
Add JUnit Jupiter to your test configuration and enable the JUnit platform.
dependencies {
testImplementation("org.junit.jupiter:junit-jupiter:5.10.2")
}
tasks.test {
useJUnitPlatform()
}Your First Test
Annotate a function with @Test. A test asserts something about your code.
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.assertEquals
class MathTest {
@Test
fun addsTwoNumbers() {
assertEquals(4, 2 + 2)
}
}Descriptive Test Names
Use @DisplayName for readable test reports without long function names.
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.DisplayName
class OrderTest {
@Test
@DisplayName("total includes tax")
fun totalWithTax() {
// ...
}
}Setup with @BeforeEach
@BeforeEach runs before every test, ideal for fresh fixtures. @AfterEach runs after each test for cleanup.
import org.junit.jupiter.api.BeforeEach
class CartTest {
private lateinit var cart: MutableList<String>
@BeforeEach
fun setUp() {
cart = mutableListOf()
}
}Class-Level Setup
@BeforeAll and @AfterAll run once for the whole class. In Kotlin they need a companion object with @JvmStatic (or a per-class lifecycle).
import org.junit.jupiter.api.BeforeAll
class DbTest {
companion object {
@BeforeAll
@JvmStatic
fun connect() {
// open shared connection
}
}
}Arrange-Act-Assert
Structure each test in three clear phases:
- Arrange — set up inputs and state
- Act — invoke the code under test
- Assert — verify the outcome
This keeps tests readable and focused.
Nested Tests
@Nested groups related tests, producing a tree-shaped report that documents behavior.
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
class StackTest {
@Nested
inner class WhenEmpty {
@Test fun isEmpty() { /* ... */ }
}
}Parameterized Tests
Run the same test with multiple inputs using @ParameterizedTest and a value source.
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
import org.junit.jupiter.api.Assertions.assertTrue
class NumberTest {
@ParameterizedTest
@ValueSource(ints = [2, 4, 6])
fun isEven(n: Int) {
assertTrue(n % 2 == 0)
}
}Disabling and Tagging
Skip a test temporarily with @Disabled, and group tests with @Tag so you can include or exclude them in CI runs.
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Test
class FlakyTest {
@Test
@Disabled("fix later")
@Tag("slow")
fun pending() { }
}A Self-Contained Example
Here is a simple pure-Kotlin function and the idea behind testing it (the test itself runs under JUnit).
fun add(a: Int, b: Int): Int = a + b
fun main() {
val result = add(2, 3)
println(if (result == 5) "PASS" else "FAIL")
}Quick Check
You need a fresh, empty list before every single test method. Which annotation fits?
Recap
You can structure JUnit5 tests:
@Testmarks tests,@DisplayNamenames them@BeforeEach/@BeforeAllhandle setup- Arrange-Act-Assert keeps tests clear
@Nestedand@ParameterizedTestorganize and scale tests
Next: assertions.
Frequently asked questions
Is the “Writing Tests with JUnit5” lesson free?
Yes — the full text of “Writing Tests with JUnit5” 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 “Writing Tests with JUnit5”?
Structure tests. 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 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Writing Tests with JUnit5” 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
- Writing Tests with JUnit5
- Assertions
- Mocking with MockK
- Coroutine Testing