0
0
JUnittesting~10 mins

@Nested inner classes in JUnit - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
ATest
BNested
CBeforeEach
DDisplayName
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Test instead of @Nested for inner classes.
Forgetting to import the @Nested annotation.
2fill in blank
medium

Complete 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'
ATest
BBeforeEach
CAfterEach
DDisabled
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Nested on methods instead of classes.
Forgetting to annotate test methods with @Test.
3fill in blank
hard

Fix 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'
AMultiplicationTests
BMultiplyTests
CMultiplicationTest
DMultiplyTest
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.
4fill in blank
hard

Fill 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'
ANested
BTest
CBeforeEach
DAfterEach
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping @Test and @Nested annotations.
Forgetting to annotate either the class or the method.
5fill in blank
hard

Fill 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'
ANested
BDisplayName
CTest
DBeforeEach
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting @DisplayName on the nested class.
Not annotating the test method with @Test.