0
0
JUnittesting~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Lifecycle Hook Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Purpose of @BeforeEach in JUnit
In JUnit testing, what is the main purpose of the @BeforeEach lifecycle hook?
ATo run code once before all tests in the class
BTo run code before each individual test method
CTo run code after each test method
DTo clean up resources after all tests finish
Attempts:
2 left
💡 Hint
Think about preparing the environment fresh for every test.
🧠 Conceptual
intermediate
2:00remaining
Role of @AfterAll in JUnit
What is the role of the @AfterAll lifecycle hook in JUnit testing?
ATo initialize resources before any tests run
BTo run cleanup code after each test method
CTo run code before each test method
DTo run cleanup code once after all tests in the class have run
Attempts:
2 left
💡 Hint
Think about releasing shared resources after all tests finish.
Predict Output
advanced
2:30remaining
Output of JUnit lifecycle hooks order
Given the following JUnit test class, what is the order of printed output when running all tests?
JUnit
import org.junit.jupiter.api.*;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class SampleTest {

    @BeforeAll
    void beforeAll() {
        System.out.println("BeforeAll");
    }

    @BeforeEach
    void beforeEach() {
        System.out.println("BeforeEach");
    }

    @Test
    void testOne() {
        System.out.println("TestOne");
    }

    @Test
    void testTwo() {
        System.out.println("TestTwo");
    }

    @AfterEach
    void afterEach() {
        System.out.println("AfterEach");
    }

    @AfterAll
    void afterAll() {
        System.out.println("AfterAll");
    }
}
ABeforeAll, BeforeEach, TestOne, AfterEach, BeforeEach, TestTwo, AfterEach, AfterAll
BBeforeEach, BeforeAll, TestOne, AfterEach, BeforeEach, TestTwo, AfterEach, AfterAll
CBeforeAll, TestOne, BeforeEach, AfterEach, TestTwo, BeforeEach, AfterEach, AfterAll
DBeforeAll, BeforeEach, TestOne, BeforeEach, TestTwo, AfterEach, AfterEach, AfterAll
Attempts:
2 left
💡 Hint
Remember the order: BeforeAll once, BeforeEach before each test, AfterEach after each test, AfterAll once.
assertion
advanced
2:00remaining
Correct assertion for resource cleanup in @AfterEach
Which assertion correctly verifies that a resource is closed after each test using @AfterEach in JUnit?
JUnit
private Resource resource;

@BeforeEach
void setup() {
    resource = new Resource();
}

@AfterEach
void cleanup() {
    resource.close();
}
AassertEquals(null, resource);
BassertNull(resource);
CassertTrue(resource.isClosed());
DassertFalse(resource.isClosed());
Attempts:
2 left
💡 Hint
Check if the resource reports it is closed after cleanup.
🔧 Debug
expert
3:00remaining
Identify the problem with lifecycle hooks causing test interference
Consider this JUnit test class where tests sometimes fail due to shared state. What is the main issue causing this?
JUnit
import static org.junit.jupiter.api.Assertions.*;

public class SharedStateTest {
    private static int counter = 0;

    @BeforeAll
    static void init() {
        counter = 0;
    }

    @Test
    void testIncrement() {
        counter++;
        assertTrue(counter > 0);
    }

    @Test
    void testReset() {
        counter = 0;
        assertEquals(0, counter);
    }
}
AUsing static variable counter causes shared state between tests leading to interference
BLifecycle hooks are not annotated with @BeforeEach causing setup failure
CIncorrect assertion in testIncrement causes failure
DMissing @AfterEach to reset counter after each test
Attempts:
2 left
💡 Hint
Think about how static variables behave across tests.