Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Test instead of @BeforeEach
Using @BeforeAll which runs only once before all tests
✗ Incorrect
The @BeforeEach annotation runs the setup method before each test method.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @BeforeEach instead of @AfterEach
Using @AfterAll which runs only once after all tests
✗ Incorrect
The @AfterEach annotation runs the cleanup method after each test method.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @BeforeEach which runs before every test instead of once
Not using companion object and @JvmStatic for @BeforeAll
✗ Incorrect
The @BeforeAll annotation runs the method once before all tests. It must be static in Kotlin, so it's inside companion object with @JvmStatic.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up @BeforeAll and @BeforeEach
Using @Test instead of lifecycle annotations
✗ Incorrect
Use @BeforeEach for setup before each test and @AfterEach for cleanup after each test.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting @Test on the test method
Using @BeforeAll or @AfterAll instead of per-test lifecycle annotations
✗ Incorrect
Use @BeforeEach for setup, @Test for the test method, and @AfterEach for cleanup.