0
0
JUnittesting~20 mins

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

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