0
0
JUnittesting~10 mins

Why lifecycle hooks manage setup and teardown in JUnit - Test Your Understanding

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 method.

JUnit
@BeforeEach
void [1]() {
    System.out.println("Setup before test");
}
Drag options to blanks, or click blank then click option'
Asetup
BtearDown
Cinitialize
Dcleanup
Attempts:
3 left
💡 Hint
Common Mistakes
Using @BeforeEach but naming the method 'tearDown' which is confusing.
Not using @BeforeEach annotation.
2fill in blank
medium

Complete the code to run teardown after each test method.

JUnit
@AfterEach
void [1]() {
    System.out.println("Cleanup after test");
}
Drag options to blanks, or click blank then click option'
AtearDown
Binitialize
Csetup
Dprepare
Attempts:
3 left
💡 Hint
Common Mistakes
Using @AfterEach but naming the method 'setup' which is misleading.
Not using @AfterEach annotation.
3fill in blank
hard

Fix the error in the lifecycle hook annotation to run setup once before all tests.

JUnit
@[1]
static void globalSetup() {
    System.out.println("Setup once before all tests");
}
Drag options to blanks, or click blank then click option'
ABeforeEach
BBeforeAll
CAfterEach
DAfterAll
Attempts:
3 left
💡 Hint
Common Mistakes
Using @BeforeEach instead of @BeforeAll for global setup.
Not making the method static with @BeforeAll.
4fill in blank
hard

Fill both blanks to run cleanup once after all tests and make the method static.

JUnit
@[1]
static void [2]() {
    System.out.println("Cleanup once after all tests");
}
Drag options to blanks, or click blank then click option'
AAfterAll
BBeforeEach
CtearDownAll
DsetupAll
Attempts:
3 left
💡 Hint
Common Mistakes
Using @BeforeEach instead of @AfterAll.
Not making the method static.
Using a method name that suggests setup instead of cleanup.
5fill in blank
hard

Fill all three blanks to create a test class with setup before each test, teardown after each test, and a test method.

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

public class SampleTest {

    @BeforeEach
    void [1]() {
        System.out.println("Setup before test");
    }

    @AfterEach
    void [2]() {
        System.out.println("Cleanup after test");
    }

    @Test
    void [3]() {
        Assertions.assertTrue(true);
    }
}
Drag options to blanks, or click blank then click option'
Asetup
BtearDown
CtestAlwaysPasses
Dinitialize
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up setup and teardown method names.
Missing the @Test annotation on the test method.
Using incorrect method names that do not reflect their purpose.