0
0
JUnittesting~20 mins

Nested class lifecycle in JUnit - Practice Problems & Coding Challenges

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

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

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

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

    @Test
    void outerTest() {
        System.out.println("Outer test");
    }

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

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

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

        @Test
        void innerTest() {
            System.out.println("Inner test");
        }
    }
}
A
Outer beforeAll
Inner beforeAll
Outer beforeEach
Outer test
Outer beforeEach
Inner beforeEach
Inner test
B
Outer beforeAll
Outer beforeEach
Outer test
Outer beforeEach
Inner beforeEach
Inner test
Inner beforeAll
C
Outer beforeAll
Outer beforeEach
Outer test
Inner beforeAll
Outer beforeEach
Inner beforeEach
Inner test
D
Inner beforeAll
Outer beforeAll
Outer beforeEach
Outer test
Outer beforeEach
Inner beforeEach
Inner test
Attempts:
2 left
💡 Hint
Remember that @BeforeAll in nested classes runs after the outer class's @BeforeAll, but before any tests in that nested class.
assertion
intermediate
1:30remaining
Correct Assertion for Nested Test Execution Count
You want to assert that exactly two tests run: one in the outer class and one in the nested class. Which assertion correctly verifies this using JUnit 5's TestInfo?
JUnit
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;

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

    int testCount = 0;

    @Test
    void outerTest() {
        testCount++;
    }

    @Nested
    class InnerTest {

        @Test
        void innerTest() {
            testCount++;
        }
    }

    @AfterAll
    void afterAll() {
        // Assertion here
    }
}
AassertEquals(2, testCount);
BassertTrue(testCount == 1);
CassertNotNull(testCount);
DassertFalse(testCount > 2);
Attempts:
2 left
💡 Hint
You want to check the exact number of tests run, not just a condition.
🔧 Debug
advanced
2:00remaining
Fix the Lifecycle Annotation Misuse in Nested Class
This JUnit 5 nested test class fails to run the @BeforeAll method inside the nested class. What is the cause?
JUnit
import org.junit.jupiter.api.*;

public class OuterTest {

    @Nested
    class InnerTest {

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

        @Test
        void test() {
            System.out.println("Inner test");
        }
    }
}
AJUnit 5 does not support @BeforeAll in nested classes.
BThe @BeforeAll method must be public to run in nested classes.
CThe nested class must be static for @BeforeAll to run.
DThe @BeforeAll method in the nested class must be static or the nested class must use @TestInstance(Lifecycle.PER_CLASS).
Attempts:
2 left
💡 Hint
Check JUnit 5 rules for @BeforeAll in non-static nested classes.
🧠 Conceptual
advanced
1:30remaining
Understanding Nested Class Test Instance Lifecycle
In JUnit 5, what is the default test instance lifecycle for nested test classes if not explicitly annotated?
AThe nested class inherits the lifecycle of the outer class automatically.
BA single instance is shared for all tests (PER_CLASS lifecycle).
CEach test method gets a new instance (PER_METHOD lifecycle).
DNested classes do not have a lifecycle and are instantiated once.
Attempts:
2 left
💡 Hint
Think about the default behavior of JUnit 5 test instances.
framework
expert
2:30remaining
Configuring Nested Class Lifecycle for Shared State
You want to share state between tests in a nested class without using static fields. Which JUnit 5 configuration achieves this correctly?
AAnnotate the outer class with @TestInstance(TestInstance.Lifecycle.PER_CLASS) only.
BAnnotate the nested class with @TestInstance(TestInstance.Lifecycle.PER_CLASS).
CUse @BeforeAll static methods to initialize shared state in the nested class.
DMake the nested class static and use static fields for shared state.
Attempts:
2 left
💡 Hint
Sharing state between tests requires a single instance of the test class.