Bird
Raised Fist0
JUnittesting~20 mins

Nested class lifecycle in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. In JUnit 5, what is the purpose of using a @Nested class inside a test class?
easy
A. To group related tests together for better organization
B. To run tests in parallel automatically
C. To disable tests temporarily
D. To create a separate test suite file

Solution

  1. Step 1: Understand the role of @Nested

    The @Nested annotation allows grouping related test methods inside inner classes to organize tests logically.
  2. Step 2: Compare other options

    The other options describe unrelated features: disabling tests temporarily, running tests in parallel automatically, and creating separate test suite files, which are not the purpose of @Nested.
  3. Final Answer:

    To group related tests together for better organization -> Option A
  4. Quick Check:

    @Nested groups tests = C [OK]
Hint: Remember: @Nested groups tests inside main class [OK]
Common Mistakes:
  • Thinking @Nested runs tests in parallel
  • Confusing @Nested with @Disabled
  • Assuming @Nested creates separate test files
2. Which lifecycle annotation is used to run a method once before all tests in a @Nested class?
easy
A. @BeforeAll
B. @AfterAll
C. @AfterEach
D. @BeforeEach

Solution

  1. Step 1: Identify lifecycle annotations for nested classes

    In JUnit 5, @BeforeAll runs once before all tests in the class, including nested classes if used inside them.
  2. Step 2: Differentiate from other annotations

    @BeforeEach runs before each test method, not once; @AfterEach and @AfterAll run after tests, so they don't fit the requirement.
  3. Final Answer:

    @BeforeAll -> Option A
  4. Quick Check:

    Run once before all tests = @BeforeAll [OK]
Hint: Before all tests in nested class use @BeforeAll [OK]
Common Mistakes:
  • Using @BeforeEach instead of @BeforeAll for once-only setup
  • Confusing @AfterAll with setup annotations
  • Not placing @BeforeAll inside nested class
3. Consider this JUnit 5 test structure:
class OuterTest {
  @BeforeAll
  static void setupOuter() { System.out.println("Outer setup"); }

  @Nested
  class InnerTest {
    @BeforeAll
    static void setupInner() { System.out.println("Inner setup"); }

    @Test
    void testOne() { System.out.println("Test one"); }
  }
}
What will be the order of printed lines when running testOne()?
medium
A. Inner setup\nOuter setup\nTest one
B. Outer setup\nInner setup\nTest one
C. Outer setup\nTest one\nInner setup
D. Test one\nOuter setup\nInner setup

Solution

  1. Step 1: Understand lifecycle order in nested classes

    The @BeforeAll in the outer class runs first, then the @BeforeAll in the nested class, followed by the test method.
  2. Step 2: Trace the print statements

    First prints "Outer setup", then "Inner setup", then "Test one" as per the method calls.
  3. Final Answer:

    Outer setup\nInner setup\nTest one -> Option B
  4. Quick Check:

    Outer before all, then inner before all, then test [OK]
Hint: Outer @BeforeAll runs before nested @BeforeAll [OK]
Common Mistakes:
  • Assuming nested @BeforeAll runs before outer
  • Mixing order of test and setup prints
  • Forgetting static requirement for @BeforeAll in nested
4. Given this nested test class, what is the error that prevents @BeforeAll from running?
class OuterTest {
  @Nested
  class InnerTest {
    @BeforeAll
    void setup() { System.out.println("Setup"); }

    @Test
    void test() { System.out.println("Test"); }
  }
}
medium
A. Missing @Test annotation on setup method
B. @BeforeAll cannot be used inside nested classes
C. @BeforeAll method must be static in nested classes
D. InnerTest class must be static

Solution

  1. Step 1: Check @BeforeAll method requirements in nested classes

    In JUnit 5, @BeforeAll methods inside nested classes must be static unless the nested class is annotated with @TestInstance(Lifecycle.PER_CLASS).
  2. Step 2: Analyze the given code

    The @BeforeAll method is non-static and the nested class is not annotated with @TestInstance, so this causes a runtime error.
  3. Final Answer:

    @BeforeAll method must be static in nested classes -> Option C
  4. Quick Check:

    @BeforeAll in nested must be static unless @TestInstance used [OK]
Hint: @BeforeAll in nested classes must be static [OK]
Common Mistakes:
  • Thinking @BeforeAll cannot be used in nested classes
  • Forgetting static keyword on @BeforeAll method
  • Confusing @BeforeAll with @BeforeEach
5. You want to run a setup method once before all tests in both the outer and nested classes without making the nested class static. Which approach correctly achieves this in JUnit 5?
hard
A. Move all tests to the outer class and remove nested classes
B. Make the nested class static and keep @BeforeAll method static
C. Use @BeforeEach instead of @BeforeAll in nested class
D. Annotate the nested class with @TestInstance(Lifecycle.PER_CLASS) and make the @BeforeAll method non-static

Solution

  1. Step 1: Understand static requirement for @BeforeAll in nested classes

    By default, @BeforeAll methods in nested classes must be static because nested classes are instance classes.
  2. Step 2: Use @TestInstance(Lifecycle.PER_CLASS) to allow non-static @BeforeAll

    Annotating the nested class with @TestInstance(Lifecycle.PER_CLASS) changes lifecycle so @BeforeAll can be non-static, allowing setup without static nested class.
  3. Step 3: Evaluate other options

    Make the nested class static and keep @BeforeAll method static requires static nested class, which is not desired. Use @BeforeEach instead of @BeforeAll in nested class changes setup frequency. Move all tests to the outer class and remove nested classes removes nested structure, losing organization.
  4. Final Answer:

    Annotate the nested class with @TestInstance(Lifecycle.PER_CLASS) and make the @BeforeAll method non-static -> Option D
  5. Quick Check:

    @TestInstance(PER_CLASS) allows non-static @BeforeAll in nested [OK]
Hint: Use @TestInstance(PER_CLASS) to avoid static @BeforeAll in nested [OK]
Common Mistakes:
  • Making nested class static when not needed
  • Using @BeforeEach instead of @BeforeAll for once-only setup
  • Removing nested classes to fix lifecycle issues
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard