0
0
JUnittesting~20 mins

Execution order of lifecycle methods in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JUnit Lifecycle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
JUnit Lifecycle Method Execution Order
What is the order of printed output when this JUnit 5 test class runs?
JUnit
import org.junit.jupiter.api.*;

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

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

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

    @Test
    void test1() {
        System.out.print("C");
    }

    @Test
    void test2() {
        System.out.print("D");
    }

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

    @AfterAll
    void afterAll() {
        System.out.print("F");
    }
}
AABCEDECF
BABCEBDEF
CABCEDEDF
DFEDBECBA
Attempts:
2 left
💡 Hint
Remember @BeforeAll and @AfterAll run once per class, @BeforeEach and @AfterEach run before and after each test.
assertion
intermediate
1:30remaining
Correct Assertion for Lifecycle Method Calls
Given a test class with @BeforeEach and @AfterEach methods that increment counters, which assertion correctly verifies that @BeforeEach ran twice after two tests?
JUnit
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;

public class CounterTest {
    static int beforeEachCount = 0;

    @BeforeEach
    void beforeEach() {
        beforeEachCount++;
    }

    @Test
    void testOne() {}

    @Test
    void testTwo() {}
}
AassertTrue(beforeEachCount == 1);
BassertNotNull(beforeEachCount);
CassertFalse(beforeEachCount > 2);
DassertEquals(2, beforeEachCount);
Attempts:
2 left
💡 Hint
Each test triggers @BeforeEach once.
🔧 Debug
advanced
2:00remaining
Identify the Lifecycle Annotation Causing the Error
This JUnit 5 test class fails to run with an error. Which lifecycle annotation is incorrectly used causing the failure?
JUnit
import org.junit.jupiter.api.*;

public class ErrorTest {

    @BeforeAll
    static void setup() {
        System.out.println("Setup");
    }

    @Test
    void test() {
        System.out.println("Test");
    }
}
A@BeforeAll method must return a value
B@Test method must be static
C@BeforeAll method must be static or the test class annotated with @TestInstance(Lifecycle.PER_CLASS)
D@Test method must have parameters
Attempts:
2 left
💡 Hint
Check JUnit 5 rules for @BeforeAll method signature.
🧠 Conceptual
advanced
1:30remaining
Understanding @TestInstance Lifecycle Impact
How does annotating a JUnit 5 test class with @TestInstance(Lifecycle.PER_CLASS) affect the execution of lifecycle methods?
AIt allows @BeforeAll and @AfterAll methods to be non-static and run once per class instance
BIt makes @BeforeEach and @AfterEach methods run only once for all tests
CIt forces all test methods to be static
DIt disables @AfterAll methods
Attempts:
2 left
💡 Hint
Think about instance creation and static method requirements.
framework
expert
3:00remaining
Order of Execution in Nested Test Classes with Lifecycle Methods
Consider this JUnit 5 test class with a nested class. What is the correct order of printed output when running all tests?
JUnit
import org.junit.jupiter.api.*;

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

    @BeforeAll
    void outerBeforeAll() {
        System.out.print("1");
    }

    @BeforeEach
    void outerBeforeEach() {
        System.out.print("2");
    }

    @Test
    void outerTest() {
        System.out.print("3");
    }

    @Nested
    @TestInstance(TestInstance.Lifecycle.PER_CLASS)
    class InnerTest {

        @BeforeAll
        void innerBeforeAll() {
            System.out.print("4");
        }

        @BeforeEach
        void innerBeforeEach() {
            System.out.print("5");
        }

        @Test
        void innerTest() {
            System.out.print("6");
        }

        @AfterEach
        void innerAfterEach() {
            System.out.print("7");
        }

        @AfterAll
        void innerAfterAll() {
            System.out.print("8");
        }
    }

    @AfterEach
    void outerAfterEach() {
        System.out.print("9");
    }

    @AfterAll
    void outerAfterAll() {
        System.out.print("0");
    }
}
A123942567980
B123942567979
C123942567981
D247885135960
Attempts:
2 left
💡 Hint
Remember lifecycle methods run in order: outer beforeAll, outer beforeEach, test, afterEach, then nested class lifecycle methods.