0
0
Kotlinprogramming~10 mins

Test fixtures and lifecycle in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to run setup before each test in Kotlin.

Kotlin
import org.junit.jupiter.api.[1]

class CalculatorTest {
    @[1]
    fun setup() {
        // setup code here
    }
}
Drag options to blanks, or click blank then click option'
ATest
BAfterEach
CBeforeEach
DBeforeAll
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Test instead of @BeforeEach
Using @BeforeAll which runs only once before all tests
2fill in blank
medium

Complete the code to run cleanup after each test in Kotlin.

Kotlin
import org.junit.jupiter.api.[1]

class CalculatorTest {
    @[1]
    fun cleanup() {
        // cleanup code here
    }
}
Drag options to blanks, or click blank then click option'
AAfterEach
BBeforeAll
CTest
DBeforeEach
Attempts:
3 left
💡 Hint
Common Mistakes
Using @BeforeEach instead of @AfterEach
Using @AfterAll which runs only once after all tests
3fill in blank
hard

Fix the error in the annotation to run code once before all tests in Kotlin.

Kotlin
import org.junit.jupiter.api.[1]

class CalculatorTest {
    companion object {
        @JvmStatic
        @[1]
        fun initAll() {
            // initialization code
        }
    }
}
Drag options to blanks, or click blank then click option'
ABeforeAll
BBeforeEach
CAfterEach
DTest
Attempts:
3 left
💡 Hint
Common Mistakes
Using @BeforeEach which runs before every test instead of once
Not using companion object and @JvmStatic for @BeforeAll
4fill in blank
hard

Fill both blanks to create a test class with setup and cleanup methods using JUnit 5 in Kotlin.

Kotlin
import org.junit.jupiter.api.[1]
import org.junit.jupiter.api.[2]

class SampleTest {
    @[1]
    fun setup() {
        // setup code
    }

    @[2]
    fun cleanup() {
        // cleanup code
    }
}
Drag options to blanks, or click blank then click option'
ABeforeEach
BTest
CAfterEach
DBeforeAll
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up @BeforeAll and @BeforeEach
Using @Test instead of lifecycle annotations
5fill in blank
hard

Fill all three blanks to define a test with setup, test method, and cleanup using JUnit 5 in Kotlin.

Kotlin
import org.junit.jupiter.api.[1]
import org.junit.jupiter.api.[2]
import org.junit.jupiter.api.[3]

class MathTest {
    @[1]
    fun setup() {
        // setup code
    }

    @[2]
    fun testAddition() {
        // test code
    }

    @[3]
    fun cleanup() {
        // cleanup code
    }
}
Drag options to blanks, or click blank then click option'
ABeforeEach
BTest
CAfterEach
DBeforeAll
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting @Test on the test method
Using @BeforeAll or @AfterAll instead of per-test lifecycle annotations