0
0
JUnittesting~10 mins

Nested class lifecycle 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 use the correct annotation for a nested test class lifecycle.

JUnit
import org.junit.jupiter.api.*;

@TestInstance(TestInstance.Lifecycle.[1])
@Nested
class InnerTest {
    @Test
    void testMethod() {
        Assertions.assertTrue(true);
    }
}
Drag options to blanks, or click blank then click option'
APER_CLASS
BSINGLETON
CSTATIC
DPER_METHOD
Attempts:
3 left
💡 Hint
Common Mistakes
Using PER_CLASS lifecycle without understanding it creates one instance for all tests.
Confusing PER_METHOD with PER_CLASS lifecycle.
2fill in blank
medium

Complete the code to annotate a nested test class that shares the same instance for all tests.

JUnit
import org.junit.jupiter.api.*;

@TestInstance(TestInstance.Lifecycle.[1])
@Nested
class SharedInstanceTest {
    int counter = 0;

    @Test
    void testIncrement() {
        counter++;
        Assertions.assertTrue(counter > 0);
    }
}
Drag options to blanks, or click blank then click option'
ASTATIC
BPER_CLASS
CSINGLETON
DPER_METHOD
Attempts:
3 left
💡 Hint
Common Mistakes
Using PER_METHOD lifecycle which creates a new instance for each test, so state is not shared.
Confusing SINGLETON or STATIC which are not valid lifecycle options.
3fill in blank
hard

Fix the error in the nested test class lifecycle annotation to make the code compile.

JUnit
import org.junit.jupiter.api.*;

@TestInstance(TestInstance.Lifecycle.[1])
@Nested
class ErrorTest {
    @Test
    void testSomething() {
        Assertions.assertEquals(1, 1);
    }
}
Drag options to blanks, or click blank then click option'
APER_CLASS
BINSTANCE
CPER_METHOD
DSINGLETON
Attempts:
3 left
💡 Hint
Common Mistakes
Using invalid lifecycle values like INSTANCE or SINGLETON.
Misspelling lifecycle enum constants.
4fill in blank
hard

Fill both blanks to create a nested test class with a per-class lifecycle and a setup method that runs once before all tests.

JUnit
import org.junit.jupiter.api.*;

@TestInstance(TestInstance.Lifecycle.[1])
@Nested
class SetupTest {

    @Before[2]
    void init() {
        System.out.println("Setup before all tests");
    }

    @Test
    void testOne() {
        Assertions.assertTrue(true);
    }
}
Drag options to blanks, or click blank then click option'
APER_CLASS
BPER_METHOD
CAll
DEach
Attempts:
3 left
💡 Hint
Common Mistakes
Using PER_METHOD lifecycle with non-static @BeforeAll causes errors.
Using @BeforeEach instead of @BeforeAll for setup before all tests.
5fill in blank
hard

Fill all three blanks to define a nested test class with per-class lifecycle, a non-static @BeforeAll method, and a test method that asserts a counter increments.

JUnit
import org.junit.jupiter.api.*;

@TestInstance(TestInstance.Lifecycle.[1])
@Nested
class CounterTest {
    int counter = 0;

    @Before[2]
    void setup() {
        counter = 1;
    }

    @Test
    void testIncrement() {
        counter[3];
        Assertions.assertEquals(2, counter);
    }
}
Drag options to blanks, or click blank then click option'
APER_METHOD
BPER_CLASS
C++
DAll
Attempts:
3 left
💡 Hint
Common Mistakes
Using PER_METHOD lifecycle with non-static @BeforeAll causes errors.
Using -- instead of ++ causing the counter to decrease.
Using @BeforeEach instead of @BeforeAll for setup before all tests.