0
0
JUnittesting~5 mins

@Nested inner classes in JUnit - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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
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.
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.
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.
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.
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.