What is the purpose of using @Nested inner classes in JUnit?
They help organize tests into logical groups inside a test class, making tests easier to read and maintain.
Click to reveal answer
beginner
How do you declare a nested test class in JUnit 5?
By annotating a non-static inner class with @Nested inside a test class.
Click to reveal answer
intermediate
Can @Nested classes access instance variables of the outer test class?
Yes, nested classes can access instance variables and methods of the outer test class, allowing shared setup.
Click to reveal answer
intermediate
What is a benefit of using @Nested classes compared to separate test classes?
They keep related tests grouped together in one file, improving readability and reducing duplication.
Click to reveal answer
intermediate
Does @Nested support lifecycle methods like @BeforeEach inside the nested class?
Yes, you can use lifecycle annotations like @BeforeEach and @AfterEach inside @Nested classes for setup and cleanup.
Click to reveal answer
What annotation marks an inner class as a nested test class in JUnit 5?
A@SubTest
B@InnerTest
C@TestClass
D@Nested
✗ Incorrect
The @Nested annotation is used to declare an inner class as a nested test class in JUnit 5.
Which of the following is a key benefit of using @Nested classes?
AThey run tests faster than separate classes.
BThey replace the need for @BeforeEach methods.
CThey allow grouping related tests inside one test class.
DThey automatically mock dependencies.
✗ Incorrect
Nested classes help organize related tests together, improving code structure and readability.
Can @Nested classes have their own @BeforeEach methods?
AOnly if they are static classes.
BYes, they can have their own lifecycle methods.
CNo, only the outer class can have lifecycle methods.
DOnly if annotated with @TestInstance.
✗ Incorrect
Nested classes can define their own lifecycle methods like @BeforeEach to set up tests.
What is the visibility requirement for a @Nested class in JUnit?
AIt must be non-static and package-private or higher.
BIt must be static and private.
CIt must be public and static.
DIt must be abstract.
✗ Incorrect
A @Nested class must be a non-static inner class with at least package-private visibility.
Which statement is true about @Nested classes accessing outer class members?
AThey can access instance variables and methods of the outer class.
BThey cannot access any members of the outer class.
CThey can only access static members of the outer class.
DThey require special annotations to access outer members.
✗ Incorrect
Nested classes have access to instance variables and methods of their outer class.
Explain how @Nested inner classes improve test organization in JUnit.
Think about how grouping tests helps keep code clean and easier to understand.
You got /4 concepts.
Describe the rules and best practices for writing @Nested classes in JUnit.
Consider class structure, annotations, and lifecycle support.
You got /4 concepts.
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
Step 1: Understand the role of @Nested
@Nested is used to group related tests inside a main test class, improving structure and readability.
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.
Final Answer:
To group related test methods together for better organization -> Option A
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
Step 1: Identify correct @Nested usage
A nested test class must be annotated with @Nested and contain test methods annotated with @Test.
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.
Final Answer:
@Nested class InnerTests { @Test void testMethod() {} } -> Option C
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
Step 1: Understand @BeforeEach in nested classes
Both the outer and nested @BeforeEach methods run before a nested test method.
Step 2: Trace method calls for testMultiply
First, outer setup prints "Setup main", then nested setup prints "Setup nested", finally testMultiply prints "Test multiply".
Final Answer:
Setup main
Setup nested
Test multiply -> Option D
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
Step 1: Check requirements for @Nested classes
@Nested classes must be non-static inner classes and can have any visibility (default or public).
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.
Final Answer:
No problem; tests will run correctly -> Option B
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?