0
0
JUnittesting~20 mins

TestInstance lifecycle per class in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JUnit PER_CLASS Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
JUnit TestInstance Lifecycle: Output of Counter Increment
Consider the following JUnit 5 test class using @TestInstance(Lifecycle.PER_CLASS). What will be the output when all tests run?
JUnit
import org.junit.jupiter.api.*;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class CounterTest {
    private int counter = 0;

    @Test
    void testOne() {
        counter++;
        System.out.println("Counter in testOne: " + counter);
    }

    @Test
    void testTwo() {
        counter++;
        System.out.println("Counter in testTwo: " + counter);
    }

    @Test
    void testThree() {
        counter++;
        System.out.println("Counter in testThree: " + counter);
    }
}
ACounter in testOne: 1\nCounter in testTwo: 2\nCounter in testThree: 3
BCounter in testOne: 1\nCounter in testTwo: 1\nCounter in testThree: 1
CCounter in testOne: 0\nCounter in testTwo: 1\nCounter in testThree: 2
DCounter in testOne: 3\nCounter in testTwo: 3\nCounter in testThree: 3
Attempts:
2 left
💡 Hint
Remember that with PER_CLASS lifecycle, the same test instance is used for all tests.
assertion
intermediate
2:00remaining
Correct Assertion for PER_CLASS Lifecycle Shared State
Given a JUnit 5 test class with @TestInstance(Lifecycle.PER_CLASS) and a shared list that tests add elements, which assertion correctly verifies the list size after two tests have run?
JUnit
import org.junit.jupiter.api.*;
import java.util.*;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class ListTest {
    private List<String> items = new ArrayList<>();

    @Test
    void addFirst() {
        items.add("first");
    }

    @Test
    void addSecond() {
        items.add("second");
    }

    @Test
    void verifySize() {
        // Which assertion below correctly checks the list size?
    }
}
AAssertions.assertEquals(2, items.size());
BAssertions.assertEquals(1, items.size());
CAssertions.assertTrue(items.isEmpty());
DAssertions.assertNull(items);
Attempts:
2 left
💡 Hint
With PER_CLASS lifecycle, the list is shared and accumulates elements added in previous tests.
🔧 Debug
advanced
2:00remaining
Debugging Unexpected Test Behavior with PER_CLASS Lifecycle
A developer notices that tests in a JUnit 5 class annotated with @TestInstance(Lifecycle.PER_CLASS) fail intermittently due to shared mutable state. What is the most likely cause?
AStatic fields are reset before each test, causing loss of data.
BJUnit creates a new instance per test, so fields are always reset causing inconsistent state.
CThe @TestInstance annotation is ignored, so tests run in parallel causing race conditions.
DTests share the same instance, so mutable fields retain changes between tests causing interference.
Attempts:
2 left
💡 Hint
Think about how PER_CLASS lifecycle affects instance variables.
🧠 Conceptual
advanced
2:00remaining
Effect of PER_CLASS Lifecycle on @BeforeEach and @AfterEach
In a JUnit 5 test class annotated with @TestInstance(Lifecycle.PER_CLASS), how often are methods annotated with @BeforeEach and @AfterEach executed?
A@BeforeEach and @AfterEach run only once for the entire test class.
B@BeforeEach and @AfterEach run once per test method invocation, using the same test instance.
C@BeforeEach and @AfterEach run once per test instance creation, which is once per test method.
D@BeforeEach and @AfterEach do not run when using PER_CLASS lifecycle.
Attempts:
2 left
💡 Hint
Consider the difference between test instance lifecycle and lifecycle of setup/teardown methods.
framework
expert
3:00remaining
Choosing TestInstance Lifecycle for Stateful Integration Tests
You are designing integration tests that require expensive setup and teardown, and tests must share state between methods. Which JUnit 5 TestInstance lifecycle is best and why?
APER_CLASS, because it runs each test in a separate thread for parallel execution.
BPER_METHOD, because it creates a new instance per test, ensuring complete isolation and no shared state.
CPER_CLASS, because it creates one instance for all tests, allowing shared state and reducing setup overhead.
DPER_METHOD, because it caches expensive setup between tests automatically.
Attempts:
2 left
💡 Hint
Think about instance reuse and shared state benefits.