Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a nested test class using JUnit 5.
JUnit
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; public class CalculatorTest { @[1] class AdditionTests { @Test void testAddPositiveNumbers() { // test code here } } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Test instead of @Nested for inner classes.
Forgetting to import the @Nested annotation.
✗ Incorrect
The @Nested annotation is used to declare an inner test class in JUnit 5.
2fill in blank
mediumComplete the code to run a test method inside a nested class.
JUnit
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; public class StringUtilsTest { @Nested class TrimTests { @[1] void testTrimSpaces() { // test code here } } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Nested on methods instead of classes.
Forgetting to annotate test methods with @Test.
✗ Incorrect
The @Test annotation marks a method as a test method in JUnit 5.
3fill in blank
hardFix the error in the nested test class declaration.
JUnit
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; public class MathTest { @Nested static class [1] { @Test void testMultiply() { // test code here } } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using static modifier on nested test classes (JUnit 5 requires non-static).
Using singular form for class name which is less clear.
✗ Incorrect
Nested test classes must be non-static in JUnit 5, but the question asks to fix the error by naming the class properly. The class name should be a plural form to indicate multiple tests, so 'MultiplicationTests' is best practice.
4fill in blank
hardFill both blanks to correctly declare and annotate a nested test class and its test method.
JUnit
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; public class ListTest { @[1] class RemoveTests { @[2] void testRemoveByIndex() { // test code here } } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping @Test and @Nested annotations.
Forgetting to annotate either the class or the method.
✗ Incorrect
The nested class must be annotated with @Nested and the test method with @Test to run properly.
5fill in blank
hardFill all three blanks to create a nested test class with a test method and a display name annotation.
JUnit
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.DisplayName; public class CalculatorTest { @[1] @[2]("Addition operations") class AdditionTests { @[3] void testAddPositiveNumbers() { // test code here } } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting @DisplayName on the nested class.
Not annotating the test method with @Test.
✗ Incorrect
The nested class is annotated with @Nested and @DisplayName to describe the group, and the test method is annotated with @Test.