0
0
Kotlinprogramming~5 mins

Test fixtures and lifecycle in Kotlin

Choose your learning style9 modes available
Introduction

Test fixtures help set up the environment before tests run and clean up after. This makes tests reliable and easy to manage.

When you need to prepare data or objects before running tests.
When you want to reset or clean resources after tests finish.
When multiple tests share the same setup steps.
When you want to avoid repeating setup code in every test.
When you want to ensure tests do not affect each other.
Syntax
Kotlin
import org.junit.jupiter.api.*

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class MyTests {

    @BeforeAll
    fun setupAll() {
        // Runs once before all tests
    }

    @BeforeEach
    fun setup() {
        // Runs before each test
    }

    @Test
    fun testExample() {
        // Test code here
    }

    @AfterEach
    fun tearDown() {
        // Runs after each test
    }

    @AfterAll
    fun tearDownAll() {
        // Runs once after all tests
    }
}

@BeforeAll and @AfterAll methods run once for the whole test class.

@BeforeEach and @AfterEach run before and after every single test method.

Examples
This example shows setup and cleanup running before and after each test.
Kotlin
import org.junit.jupiter.api.*

class SimpleTest {

    @BeforeEach
    fun prepare() {
        println("Setup before each test")
    }

    @Test
    fun testOne() {
        println("Running test one")
    }

    @Test
    fun testTwo() {
        println("Running test two")
    }

    @AfterEach
    fun cleanup() {
        println("Cleanup after each test")
    }
}
This example shows setup and cleanup running once for the whole test class.
Kotlin
import org.junit.jupiter.api.*

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class ClassLevelTest {

    @BeforeAll
    fun initAll() {
        println("Setup once before all tests")
    }

    @AfterAll
    fun cleanupAll() {
        println("Cleanup once after all tests")
    }

    @Test
    fun testA() {
        println("Test A running")
    }

    @Test
    fun testB() {
        println("Test B running")
    }
}
Sample Program

This program shows a simple Calculator test class. It creates a Calculator before each test and clears it after. It prints messages to show the test lifecycle.

Kotlin
import org.junit.jupiter.api.*

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class CalculatorTest {

    private var calculator: Calculator? = null

    @BeforeAll
    fun setupAll() {
        println("Starting Calculator tests")
    }

    @BeforeEach
    fun setup() {
        calculator = Calculator()
        println("Calculator created")
    }

    @Test
    fun testAdd() {
        val result = calculator?.add(2, 3)
        println("Add result: $result")
        Assertions.assertEquals(5, result)
    }

    @AfterEach
    fun tearDown() {
        calculator = null
        println("Calculator cleared")
    }

    @AfterAll
    fun tearDownAll() {
        println("Finished Calculator tests")
    }
}

class Calculator {
    fun add(a: Int, b: Int): Int = a + b
}
OutputSuccess
Important Notes

Use @BeforeAll and @AfterAll for expensive setup or cleanup done once.

Use @BeforeEach and @AfterEach to prepare fresh state for every test.

Remember to annotate the test class with @TestInstance(TestInstance.Lifecycle.PER_CLASS) if you want non-static @BeforeAll and @AfterAll methods.

Summary

Test fixtures prepare and clean the test environment.

@BeforeAll/@AfterAll run once per class; @BeforeEach/@AfterEach run per test.

They help keep tests independent and easy to maintain.