Challenge - 5 Problems
JUnit PER_CLASS Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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); } }
Attempts:
2 left
💡 Hint
Remember that with PER_CLASS lifecycle, the same test instance is used for all tests.
✗ Incorrect
With @TestInstance(Lifecycle.PER_CLASS), JUnit creates one instance of the test class and reuses it for all test methods. The counter variable is incremented cumulatively across tests, so the output shows increasing values.
❓ assertion
intermediate2: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? } }
Attempts:
2 left
💡 Hint
With PER_CLASS lifecycle, the list is shared and accumulates elements added in previous tests.
✗ Incorrect
Since the same instance is used for all tests, the list keeps elements added in previous tests. After addFirst and addSecond run, the list size is 2.
🔧 Debug
advanced2: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?
Attempts:
2 left
💡 Hint
Think about how PER_CLASS lifecycle affects instance variables.
✗ Incorrect
With PER_CLASS lifecycle, the same test instance is reused, so mutable fields keep their values across tests. If tests modify these fields without resetting, it causes flaky failures.
🧠 Conceptual
advanced2: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?
Attempts:
2 left
💡 Hint
Consider the difference between test instance lifecycle and lifecycle of setup/teardown methods.
✗ Incorrect
Even with PER_CLASS lifecycle, @BeforeEach and @AfterEach run before and after each test method, but the same test instance is reused.
❓ framework
expert3: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?
Attempts:
2 left
💡 Hint
Think about instance reuse and shared state benefits.
✗ Incorrect
PER_CLASS lifecycle creates one test instance for all tests, so state can be shared and setup/teardown can be done once, improving performance for expensive operations.