0
0
JUnittesting

Nested class lifecycle in JUnit - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a nested test class in JUnit?
A nested test class is a static or non-static inner class inside a test class used to group related tests together for better organization and readability.
Click to reveal answer
intermediate
How does the lifecycle of a nested test class differ from the outer test class in JUnit?
Each nested test class has its own lifecycle: its @BeforeAll and @AfterAll methods run before and after all tests in that nested class, separate from the outer class lifecycle.
Click to reveal answer
beginner
Which JUnit annotation is used to define a nested test class?
The @Nested annotation is used to mark a class inside a test class as a nested test class.
Click to reveal answer
intermediate
Can @BeforeAll methods in nested classes be non-static in JUnit 5?
Yes, in nested test classes, @BeforeAll and @AfterAll methods can be non-static because JUnit creates a new instance of the nested class for running tests.
Click to reveal answer
beginner
Why use nested test classes in JUnit?
Nested test classes help organize tests into logical groups, making tests easier to read and maintain, especially when testing different scenarios or states of the same class.
Click to reveal answer
Which annotation marks a nested test class in JUnit?
A@InnerTest
B@Nested
C@TestClass
D@SubTest
In JUnit, when are @BeforeAll methods in a nested class executed?
ABefore all tests in the nested class
BBefore all tests in the outer class
CBefore each test method in the nested class
DAfter all tests in the nested class
Can @BeforeAll methods in nested classes be non-static in JUnit 5?
AOnly if the outer class is static
BNo, never
CYes, always
DYes, because JUnit creates a new instance of the nested class
What is the main benefit of using nested test classes?
ABetter test organization and readability
BAutomatic parallel test running
CFaster test execution
DSimplifies mocking dependencies
Which lifecycle methods are scoped to a nested test class?
A@BeforeEach and @AfterEach only
B@BeforeAll and @AfterAll only
C@BeforeAll, @AfterAll, @BeforeEach, and @AfterEach
DNone of the above
Explain how the lifecycle of nested test classes works in JUnit and how it differs from the outer test class lifecycle.
Think about how setup and teardown happen inside nested classes compared to the outer class.
You got /4 concepts.
    Describe the advantages of using nested test classes in organizing your JUnit tests.
    Consider how nested classes help when testing multiple states or behaviors of the same class.
    You got /4 concepts.