0
0
JUnittesting~10 mins

Execution order of lifecycle methods in JUnit - Interactive Code Practice

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

Complete the code to annotate a method that runs once before all tests in a JUnit 5 test class.

JUnit
@[1]
static void setupAll() {
    System.out.println("Runs once before all tests");
}
Drag options to blanks, or click blank then click option'
ABeforeAll
BBeforeEach
CAfterAll
DAfterEach
Attempts:
3 left
💡 Hint
Common Mistakes
Using @BeforeEach instead of @BeforeAll
Forgetting to make the method static
2fill in blank
medium

Complete the code to annotate a method that runs before each test method in JUnit 5.

JUnit
@[1]
void setup() {
    System.out.println("Runs before each test");
}
Drag options to blanks, or click blank then click option'
ABeforeAll
BBeforeEach
CAfterAll
DAfterEach
Attempts:
3 left
💡 Hint
Common Mistakes
Using @BeforeAll instead of @BeforeEach
Making the method static (should be instance method)
3fill in blank
hard

Fix the error in the code to correctly annotate a method that runs once after all tests in JUnit 5.

JUnit
@[1]
static void tearDownAll() {
    System.out.println("Runs once after all tests");
}
Drag options to blanks, or click blank then click option'
ABeforeAll
BAfterEach
CAfterAll
DBeforeEach
Attempts:
3 left
💡 Hint
Common Mistakes
Using @AfterEach instead of @AfterAll
Not making the method static
4fill in blank
hard

Fill both blanks to create a JUnit 5 test class with methods that run before all tests and after each test.

JUnit
import org.junit.jupiter.api.*;

public class ExampleTest {

    @[1]
    static void initAll() {
        System.out.println("Init before all tests");
    }

    @[2]
    void tearDown() {
        System.out.println("Cleanup after each test");
    }
}
Drag options to blanks, or click blank then click option'
ABeforeAll
BBeforeEach
CAfterEach
DAfterAll
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up @AfterEach and @AfterAll
Forgetting static on @BeforeAll method
5fill in blank
hard

Fill all three blanks to create a JUnit 5 test class with methods that run before each test, after each test, and once after all tests.

JUnit
import org.junit.jupiter.api.*;

public class LifecycleTest {

    @[1]
    void setup() {
        System.out.println("Before each test");
    }

    @[2]
    void cleanup() {
        System.out.println("After each test");
    }

    @[3]
    static void finish() {
        System.out.println("After all tests");
    }
}
Drag options to blanks, or click blank then click option'
ABeforeEach
BAfterEach
CAfterAll
DBeforeAll
Attempts:
3 left
💡 Hint
Common Mistakes
Using @BeforeAll instead of @BeforeEach for setup
Not making @AfterAll method static