Bird
Raised Fist0
JUnittesting~20 mins

@Nested inner classes in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Nested Class Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of nested test execution order
Consider the following JUnit 5 test class with @Nested inner classes. What will be the order of printed output when running the tests?
JUnit
import org.junit.jupiter.api.*;

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

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

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

    @Nested
    class InnerTest {

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

        @Test
        void innerTest() {
            System.out.println("Inner Test");
        }
    }
}
A
BeforeAll Outer
BeforeAll Inner
Outer Test
Inner Test
B
BeforeAll Outer
BeforeAll Inner
Inner Test
Outer Test
C
BeforeAll Inner
BeforeAll Outer
Outer Test
Inner Test
D
BeforeAll Outer
Outer Test
BeforeAll Inner
Inner Test
Attempts:
2 left
💡 Hint
Remember that @BeforeAll in nested classes runs after the outer @BeforeAll and before tests in that nested class.
❓ assertion
intermediate
1:30remaining
Correct assertion for nested test result
Given a nested test class that sets a boolean flag to true in its test method, which assertion correctly verifies the flag is true after the nested test runs?
JUnit
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class FlagTest {
    boolean flag = false;

    @Nested
    class Inner {
        @Test
        void setFlag() {
            flag = true;
        }
    }
}
AassertEquals(true, flag);
BassertTrue(flag);
CassertFalse(flag);
DassertNull(flag);
Attempts:
2 left
💡 Hint
Use the assertion that checks if a boolean is true.
🔧 Debug
advanced
2:00remaining
Identify the cause of @BeforeAll not running in nested class
Why does the @BeforeAll method inside the nested class not execute when running the tests?
JUnit
import org.junit.jupiter.api.*;

public class SampleTest {

    @Nested
    class Inner {

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

        @Test
        void test() {
            System.out.println("Test Inner");
        }
    }
}
AThe @BeforeAll method must be annotated with @Test as well.
B@BeforeAll methods are ignored in nested classes by design.
C@BeforeAll methods in nested classes must be static unless the outer class uses @TestInstance(Lifecycle.PER_CLASS).
DThe nested class must be public for @BeforeAll to run.
Attempts:
2 left
💡 Hint
Check the method modifiers and test instance lifecycle.
🧠 Conceptual
advanced
1:30remaining
Purpose of @Nested in JUnit 5
What is the main purpose of using @Nested inner classes in JUnit 5 tests?
ATo group related tests together and provide better test organization and readability.
BTo run tests in parallel automatically.
CTo allow tests to share static variables only.
DTo disable tests temporarily without removing them.
Attempts:
2 left
💡 Hint
Think about how nested classes help structure tests.
❓ framework
expert
2:30remaining
Behavior of @Nested with @TestInstance(Lifecycle.PER_CLASS)
In a JUnit 5 test class annotated with @TestInstance(Lifecycle.PER_CLASS), how does this affect the execution of @BeforeAll methods in @Nested inner classes?
A@BeforeAll methods in nested classes can be non-static and will run once per class instance.
B@BeforeAll methods must still be static even with @TestInstance(Lifecycle.PER_CLASS).
C@BeforeAll methods are ignored in nested classes when using @TestInstance(Lifecycle.PER_CLASS).
D@BeforeAll methods run before each test method regardless of static or instance.
Attempts:
2 left
💡 Hint
Consider how test instance lifecycle changes method requirements.

Practice

(1/5)
1. What is the main purpose of using @Nested inner classes in JUnit tests?
easy
A. To group related test methods together for better organization
B. To run tests in parallel automatically
C. To replace the main test class entirely
D. To create test methods without annotations

Solution

  1. Step 1: Understand the role of @Nested

    @Nested is used to group related tests inside a main test class, improving structure and readability.
  2. Step 2: Compare options

    Only To group related test methods together for better organization correctly describes grouping related tests. Other options describe unrelated or incorrect behaviors.
  3. Final Answer:

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

    @Nested groups tests = A [OK]
Hint: Remember: @Nested groups tests inside main class [OK]
Common Mistakes:
  • Thinking @Nested runs tests in parallel
  • Assuming @Nested replaces main test class
  • Believing @Nested removes need for annotations
2. Which of the following is the correct way to declare a nested test class in JUnit 5?
easy
A. public class NestedTests { @Test void testMethod() {} }
B. @Test class InnerTests { void testMethod() {} }
C. @Nested class InnerTests { @Test void testMethod() {} }
D. class InnerTests { @Nested void testMethod() {} }

Solution

  1. Step 1: Identify correct @Nested usage

    A nested test class must be annotated with @Nested and contain test methods annotated with @Test.
  2. Step 2: Check each option

    @Nested class InnerTests { @Test void testMethod() {} } correctly shows @Nested on class and @Test on method. Others misuse annotations or omit them.
  3. Final Answer:

    @Nested class InnerTests { @Test void testMethod() {} } -> Option C
  4. Quick Check:

    @Nested on class + @Test on method = B [OK]
Hint: @Nested annotates class, @Test annotates methods [OK]
Common Mistakes:
  • Putting @Test on class instead of method
  • Missing @Nested on inner class
  • Annotating methods with @Nested
3. Consider the following JUnit 5 test code snippet:
class CalculatorTest {
  @BeforeEach void setup() { System.out.println("Setup main"); }

  @Test void testAdd() { System.out.println("Test add"); }

  @Nested
  class MultiplyTests {
    @BeforeEach void setup() { System.out.println("Setup nested"); }

    @Test void testMultiply() { System.out.println("Test multiply"); }
  }
}
What will be the output when running the testMultiply method?
medium
A. Setup main Test multiply
B. Setup nested Test multiply
C. Test multiply
D. Setup main Setup nested Test multiply

Solution

  1. Step 1: Understand @BeforeEach in nested classes

    Both the outer and nested @BeforeEach methods run before a nested test method.
  2. Step 2: Trace method calls for testMultiply

    First, outer setup prints "Setup main", then nested setup prints "Setup nested", finally testMultiply prints "Test multiply".
  3. Final Answer:

    Setup main Setup nested Test multiply -> Option D
  4. Quick Check:

    Outer + nested setup run before nested test = D [OK]
Hint: Both outer and nested @BeforeEach run before nested tests [OK]
Common Mistakes:
  • Assuming only nested setup runs
  • Ignoring outer setup for nested tests
  • Expecting only test method output
4. Given the following JUnit 5 test code:
class OuterTest {
  @Nested
  class InnerTest {
    @Test void testSomething() {}
  }

  @Test void testOuter() {}
}
Which of the following is a problem that will cause the tests NOT to run as expected?
medium
A. InnerTest class is not declared static
B. No problem; tests will run correctly
C. InnerTest class is missing @Test annotation
D. InnerTest class is not public

Solution

  1. Step 1: Check requirements for @Nested classes

    @Nested classes must be non-static inner classes and can have any visibility (default or public).
  2. Step 2: Analyze given code

    InnerTest is a non-static inner class with @Nested annotation and contains a @Test method. OuterTest has a @Test method too.
  3. Final Answer:

    No problem; tests will run correctly -> Option B
  4. Quick Check:

    @Nested inner classes run fine non-static = C [OK]
Hint: Nested classes should be non-static; visibility is flexible [OK]
Common Mistakes:
  • Thinking nested classes must be static
  • Expecting @Test on nested class itself
  • Assuming nested class must be public
5. You want to organize tests for a Calculator class using JUnit 5. You decide to use @Nested classes to separate addition and subtraction tests. Which of the following code snippets correctly applies @Nested classes with proper setup methods so that the main setup runs before each nested test setup?
hard
A. class CalculatorTest { @BeforeEach void setup() { System.out.println("Main setup"); } @Nested class AddTests { @BeforeEach void setup() { System.out.println("Add setup"); } @Test void testAdd() {} } @Nested class SubtractTests { @BeforeEach void setup() { System.out.println("Subtract setup"); } @Test void testSubtract() {} } }
B. class CalculatorTest { @Nested class AddTests { @BeforeEach void setup() { System.out.println("Add setup"); } @Test void testAdd() {} } @Nested class SubtractTests { @BeforeEach void setup() { System.out.println("Subtract setup"); } @Test void testSubtract() {} } @BeforeEach void setup() { System.out.println("Main setup"); } }
C. class CalculatorTest { @BeforeAll static void setup() { System.out.println("Main setup"); } @Nested class AddTests { @BeforeEach void setup() { System.out.println("Add setup"); } @Test void testAdd() {} } @Nested class SubtractTests { @BeforeEach void setup() { System.out.println("Subtract setup"); } @Test void testSubtract() {} } }
D. class CalculatorTest { @BeforeEach void setup() { System.out.println("Main setup"); } @Nested static class AddTests { @BeforeEach void setup() { System.out.println("Add setup"); } @Test void testAdd() {} } @Nested static class SubtractTests { @BeforeEach void setup() { System.out.println("Subtract setup"); } @Test void testSubtract() {} } }

Solution

  1. Step 1: Understand setup execution order

    @BeforeEach in outer class runs before each nested class's @BeforeEach method for every test.
  2. Step 2: Analyze options for correct setup and class structure

    class CalculatorTest { @BeforeEach void setup() { System.out.println("Main setup"); } @Nested class AddTests { @BeforeEach void setup() { System.out.println("Add setup"); } @Test void testAdd() {} } @Nested class SubtractTests { @BeforeEach void setup() { System.out.println("Subtract setup"); } @Test void testSubtract() {} } } has non-static nested classes and main @BeforeEach before nested @BeforeEach, ensuring main setup runs first. class CalculatorTest { @Nested class AddTests { @BeforeEach void setup() { System.out.println("Add setup"); } @Test void testAdd() {} } @Nested class SubtractTests { @BeforeEach void setup() { System.out.println("Subtract setup"); } @Test void testSubtract() {} } @BeforeEach void setup() { System.out.println("Main setup"); } } places main setup after nested classes, which is allowed but less clear. class CalculatorTest { @BeforeAll static void setup() { System.out.println("Main setup"); } @Nested class AddTests { @BeforeEach void setup() { System.out.println("Add setup"); } @Test void testAdd() {} } @Nested class SubtractTests { @BeforeEach void setup() { System.out.println("Subtract setup"); } @Test void testSubtract() {} } } uses @BeforeAll which runs once, not before each test. class CalculatorTest { @BeforeEach void setup() { System.out.println("Main setup"); } @Nested static class AddTests { @BeforeEach void setup() { System.out.println("Add setup"); } @Test void testAdd() {} } @Nested static class SubtractTests { @BeforeEach void setup() { System.out.println("Subtract setup"); } @Test void testSubtract() {} } } uses static nested classes, which disables outer @BeforeEach running before nested ones.
  3. Final Answer:

    Option A with non-static nested classes and main @BeforeEach before nested setups -> Option A
  4. Quick Check:

    Non-static nested + outer @BeforeEach before nested = A [OK]
Hint: Use non-static nested classes; outer @BeforeEach runs before nested [OK]
Common Mistakes:
  • Making nested classes static disables outer @BeforeEach
  • Using @BeforeAll instead of @BeforeEach for setup
  • Placing main setup after nested classes causing confusion
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