0
0
JUnittesting~10 mins

TestInstance lifecycle per class 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 set the test instance lifecycle to per class.

JUnit
@TestInstance([1])
public class CalculatorTest {
    // test methods
}
Drag options to blanks, or click blank then click option'
ATestInstance.Lifecycle.PER_CLASS
BTestInstance.Lifecycle.PER_METHOD
CTestInstance.Lifecycle.SINGLETON
DTestInstance.Lifecycle.PER_TEST
Attempts:
3 left
💡 Hint
Common Mistakes
Using PER_METHOD creates a new instance for each test method.
Using SINGLETON or PER_TEST are not valid lifecycle options.
2fill in blank
medium

Complete the code to import the correct TestInstance annotation.

JUnit
import org.junit.jupiter.api.[1];

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class CalculatorTest {
    // test methods
}
Drag options to blanks, or click blank then click option'
ABeforeEach
BTest
CTestInstance
DAfterAll
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Test instead of TestInstance.
Confusing lifecycle annotations with test method annotations.
3fill in blank
hard

Fix the error in the lifecycle annotation usage.

JUnit
@TestInstance(TestInstance.Lifecycle.[1])
public class CalculatorTest {
    // test methods
}
Drag options to blanks, or click blank then click option'
APER_METHOD
BPER_CLASS
CPER_INSTANCE
DPER_TEST
Attempts:
3 left
💡 Hint
Common Mistakes
Using invalid enum values like PER_INSTANCE or PER_TEST.
Confusing PER_METHOD with PER_CLASS.
4fill in blank
hard

Fill both blanks to complete the test class with lifecycle per class and a test method.

JUnit
@TestInstance([1])
public class CalculatorTest {

    @Test
    public void testAdd() {
        int result = 2 + 3;
        assertEquals([2], result);
    }
}
Drag options to blanks, or click blank then click option'
ATestInstance.Lifecycle.PER_CLASS
B5
C4
DTestInstance.Lifecycle.PER_METHOD
Attempts:
3 left
💡 Hint
Common Mistakes
Using PER_METHOD instead of PER_CLASS for lifecycle.
Asserting the wrong expected value in assertEquals.
5fill in blank
hard

Fill all three blanks to complete the test class with lifecycle per class, a test method, and an import.

JUnit
import org.junit.jupiter.api.[1];
import static org.junit.jupiter.api.Assertions.[2];

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class CalculatorTest {

    @Test
    public void testMultiply() {
        int result = 3 * 4;
        [3](12, result);
    }
}
Drag options to blanks, or click blank then click option'
ATestInstance
BassertEquals
CassertTrue
DassertFalse
Attempts:
3 left
💡 Hint
Common Mistakes
Importing wrong assertion methods like assertTrue or assertFalse.
Forgetting to import TestInstance.