CodeBlanks Logo Icon CodeBlanks

@BeforeEach

Learn fixtures with @BeforeEach and @AfterEach.

@BeforeEach / @AfterEach

The @BeforeEach annotation marks a method that runs before each test method. @AfterEach runs after each test. They are used to do the common setup and teardown for tests; so each test starts from a clean state.

class CalculatorTest {
    lateinit var c: Calculator

    @BeforeEach
    fun setup() { c = Calculator() }

    @AfterEach
    fun cleanup() { /* clean up */ }
}

Here a fresh Calculator instance is created before each test; after the test cleanup runs. This prevents tests from affecting each other. lateinit var is handy for variables assigned in setup; accessing it before assignment throws an exception.

The setup and teardown pattern

Common setup ensures each test starts from the same state. For example a database connection, file or list is prepared before each test; after the test it is closed or cleaned. This keeps tests independent and repeatable.

@BeforeEach
fun setup() {
    counter = 0
}

@Test
fun test1() {
    counter++
    assertEquals(1, counter)
}

Here counter is reset before each test; so test1 and test2 do not affect each other. The setup method is usually called setup, the teardown method cleanup or teardown. For heavy resources (database, network) it is important to consider setup cost.

Practice Apply the beforeEach pattern

Define class Calculator; inside it add var total: Int = 0 and fun add(n: Int) { total += n }. Then define a helper fun beforeEach(c: Calculator) whose body does c.total = 0. In main create val c = Calculator(); call beforeEach(c); c.add(5); check(c.total == 5); call beforeEach(c); c.add(3); check(c.total == 3); print println("all passed").

Starter code

Main.kt
class Calculator {
    var total: Int = 0
    fun add(n: Int) { total += n }
}

// beforeEach function: reset the Calculator total
// main: do two "tests" — call beforeEach before each
// test 1: add(5), check total==5; test 2: add(3), check total==3
// print "all passed"
fun main() {
    // write here
}

Sample solution

Main.kt
fun beforeEach(c: Calculator) { c.total = 0 }

fun main() {
    val c = Calculator()
    beforeEach(c)
    c.add(5)
    check(c.total == 5)
    beforeEach(c)
    c.add(3)
    check(c.total == 3)
    println("all passed")
}

Expected output

all passed

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 When does @BeforeEach run?

  • Once before all tests
  • Once after all tests
  • After each test
  • Before each test Correct answer

Explanation: @BeforeEach runs before each test method.

2 Match the setup/teardown concepts

Term Meaning
@BeforeEach Before each test
@AfterEach After each test
setup() Setup method
cleanup() Teardown method

3 The setup annotation

Question
____
fun setup() { c = Calculator() }
Answer
@BeforeEach
fun setup() { c = Calculator() }

Explanation: @BeforeEach marks the method that runs before each test.

All lessons in Intermediate