Bird
Raised Fist0
Kotlinprogramming~10 mins

Test fixtures and lifecycle in Kotlin - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Test fixtures and lifecycle
Start Test Class
↓
BeforeAll: Setup shared resources
↓
For each Test Method
↓
BeforeEach: Setup test fixture
↓
Run Test Method
↓
AfterEach: Cleanup test fixture
↓
Repeat for next Test Method
↓
AfterAll: Cleanup shared resources
↓
End Test Class
This flow shows how test fixtures are set up and cleaned up before and after tests run in a Kotlin test class.
Execution Sample
Kotlin
import org.junit.jupiter.api.*

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class CalculatorTest {
    @BeforeAll
    fun setupAll() { println("Setup All") }

    @BeforeEach
    fun setup() { println("Setup Each") }

    @Test
    fun testAdd() { println("Test Add") }

    @AfterEach
    fun tearDown() { println("Tear Down Each") }

    @AfterAll
    fun tearDownAll() { println("Tear Down All") }
}
This code runs setup and teardown methods around each test and once for all tests.
Execution Table
StepAnnotationActionOutput
1@BeforeAllRun once before all testsSetup All
2@BeforeEachRun before testAddSetup Each
3@TestRun testAdd methodTest Add
4@AfterEachRun after testAddTear Down Each
5@AfterAllRun once after all testsTear Down All
6EndAll tests finishedNo more output
💡 All test lifecycle methods have run, tests complete.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
Test Class StateNot startedSetup All doneSetup Each doneTest Add doneTear Down Each doneTear Down All doneFinished
Key Moments - 3 Insights
Why does @BeforeAll run only once but @BeforeEach runs before every test?
@BeforeAll is for shared setup before any tests run (see Step 1), while @BeforeEach prepares the environment fresh for each test (see Step 2).
What happens if there are multiple test methods?
The @BeforeEach and @AfterEach methods run before and after each test method, repeating steps 2-4 for each test.
When does @AfterAll run?
@AfterAll runs once after all tests and their fixtures finish (see Step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is printed at Step 3?
ATest Add
BSetup Each
CTear Down Each
DSetup All
💡 Hint
Check the 'Output' column for Step 3 in the execution_table.
At which step does the shared setup for all tests happen?
AStep 2
BStep 1
CStep 4
DStep 5
💡 Hint
Look for @BeforeAll annotation in the execution_table.
If a second test method is added, which lifecycle methods run again before it?
A@BeforeAll only
B@AfterAll only
C@BeforeEach and @AfterEach
DNo lifecycle methods run again
💡 Hint
Refer to key_moments about multiple test methods and steps 2-4 in execution_table.
Concept Snapshot
Test fixtures in Kotlin use annotations:
@BeforeAll runs once before all tests
@BeforeEach runs before each test
@Test marks test methods
@AfterEach runs after each test
@AfterAll runs once after all tests
This manages setup and cleanup cleanly.
Full Transcript
This visual trace shows how Kotlin test fixtures and lifecycle annotations work. First, @BeforeAll runs once to set up shared resources. Then for each test method, @BeforeEach runs to prepare the test environment, followed by the test method itself marked with @Test. After the test, @AfterEach cleans up. After all tests finish, @AfterAll runs once to clean up shared resources. The execution table shows the order and output of these steps. Variables track the test class state changing from not started to finished. Key moments clarify why some methods run once and others multiple times. The quiz tests understanding of the lifecycle order and method roles.

Practice

(1/5)
1. What is the main purpose of test fixtures in Kotlin testing?
easy
A. To prepare and clean the test environment before and after tests
B. To write the actual test cases
C. To generate random test data automatically
D. To compile the Kotlin code before running tests

Solution

  1. Step 1: Understand what test fixtures do

    Test fixtures set up the environment needed for tests and clean it afterward to avoid side effects.
  2. Step 2: Identify the correct purpose

    Among the options, only preparing and cleaning the test environment matches the role of test fixtures.
  3. Final Answer:

    To prepare and clean the test environment before and after tests -> Option A
  4. Quick Check:

    Test fixtures = prepare and clean environment [OK]
Hint: Remember: fixtures setup and cleanup tests [OK]
Common Mistakes:
  • Confusing fixtures with test case logic
  • Thinking fixtures generate test data
  • Assuming fixtures compile code
2. Which annotation in Kotlin testing runs a method once before all tests in a class?
easy
A. @AfterEach
B. @BeforeAll
C. @BeforeEach
D. @AfterAll

Solution

  1. Step 1: Recall lifecycle annotations

    @BeforeAll runs once before all tests; @BeforeEach runs before each test.
  2. Step 2: Match annotation to description

    The annotation that runs once before all tests is @BeforeAll.
  3. Final Answer:

    @BeforeAll -> Option B
  4. Quick Check:

    @BeforeAll = run once before all tests [OK]
Hint: BeforeAll runs once before all tests [OK]
Common Mistakes:
  • Mixing @BeforeEach with @BeforeAll
  • Confusing @AfterAll with @BeforeAll
  • Using @AfterEach instead of @BeforeAll
3. Given the following Kotlin test class, what will be the output order of the print statements?
class SampleTest {
    @BeforeAll
    fun setupAll() = println("SetupAll")

    @BeforeEach
    fun setup() = println("Setup")

    @Test
    fun test1() = println("Test1")

    @Test
    fun test2() = println("Test2")

    @AfterEach
    fun tearDown() = println("TearDown")

    @AfterAll
    fun tearDownAll() = println("TearDownAll")
}
medium
A. Setup Test1 Setup Test2 TearDown TearDownAll SetupAll
B. Setup Test1 TearDown Setup Test2 TearDown SetupAll TearDownAll
C. SetupAll Setup Test1 Test2 TearDown TearDownAll
D. SetupAll Setup Test1 TearDown Setup Test2 TearDown TearDownAll

Solution

  1. Step 1: Understand lifecycle order

    @BeforeAll runs once before all tests, then for each test @BeforeEach, test, @AfterEach run, and finally @AfterAll runs once after all tests.
  2. Step 2: Trace the print statements

    Output order: SetupAll (once), then for test1: Setup, Test1, TearDown; for test2: Setup, Test2, TearDown; finally TearDownAll.
  3. Final Answer:

    SetupAll Setup Test1 TearDown Setup Test2 TearDown TearDownAll -> Option D
  4. Quick Check:

    Lifecycle order = D [OK]
Hint: BeforeAll once, BeforeEach per test, AfterEach per test, AfterAll once [OK]
Common Mistakes:
  • Assuming @BeforeEach runs once only
  • Mixing order of AfterEach and AfterAll
  • Forgetting @BeforeAll and @AfterAll run once
4. Identify the error in this Kotlin test fixture code snippet:
@BeforeAll
fun setup() {
    println("Setup")
}

@Test
fun testExample() {
    println("Test")
}
medium
A. @BeforeAll method must be static or inside a companion object
B. setup() should return a value
C. Missing @Test annotation on setup()
D. testExample() must be private

Solution

  1. Step 1: Recall @BeforeAll requirements in Kotlin

    @BeforeAll methods must be static or placed inside a companion object to run once before all tests.
  2. Step 2: Check the code snippet

    The setup() method is annotated with @BeforeAll but is not static nor inside a companion object, causing an error.
  3. Final Answer:

    @BeforeAll method must be static or inside a companion object -> Option A
  4. Quick Check:

    @BeforeAll requires static or companion object [OK]
Hint: @BeforeAll needs static or companion object [OK]
Common Mistakes:
  • Thinking @BeforeAll can be a normal instance method
  • Assuming test methods must be private
  • Believing setup() needs a return value
5. You want to ensure a database connection is opened once before all tests and closed once after all tests in a Kotlin test class. Which is the best way to implement this using test fixtures?
hard
A. Use @BeforeEach to open and @AfterEach to close the connection
B. Open the connection in the constructor and close it in a test method
C. Use @BeforeAll inside a companion object to open and @AfterAll inside the companion object to close
D. Use @BeforeAll and @AfterAll as instance methods without companion object

Solution

  1. Step 1: Understand lifecycle for shared resources

    Resources like database connections should open once before all tests and close once after all tests to save time and avoid repeated setup.
  2. Step 2: Apply Kotlin @BeforeAll/@AfterAll rules

    In Kotlin, @BeforeAll and @AfterAll must be inside a companion object or be static to run once per class.
  3. Step 3: Evaluate options

    Use @BeforeAll inside a companion object to open and @AfterAll inside the companion object to close correctly uses @BeforeAll and @AfterAll inside a companion object for one-time setup and teardown.
  4. Final Answer:

    Use @BeforeAll inside a companion object to open and @AfterAll inside the companion object to close -> Option C
  5. Quick Check:

    Shared setup/teardown = @BeforeAll/@AfterAll in companion object [OK]
Hint: Use companion object for @BeforeAll/@AfterAll in Kotlin [OK]
Common Mistakes:
  • Using @BeforeEach/@AfterEach for expensive shared setup
  • Placing @BeforeAll/@AfterAll outside companion object
  • Opening connection in constructor instead of fixture
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard