Challenge - 5 Problems
Lifecycle Hook Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Purpose of @BeforeEach in JUnit
In JUnit testing, what is the main purpose of the @BeforeEach lifecycle hook?
Attempts:
2 left
💡 Hint
Think about preparing the environment fresh for every test.
✗ Incorrect
The @BeforeEach hook runs before every test method to set up a clean state. This ensures tests do not affect each other.
🧠 Conceptual
intermediate2:00remaining
Role of @AfterAll in JUnit
What is the role of the @AfterAll lifecycle hook in JUnit testing?
Attempts:
2 left
💡 Hint
Think about releasing shared resources after all tests finish.
✗ Incorrect
@AfterAll runs once after all tests complete, ideal for releasing shared resources like database connections.
❓ Predict Output
advanced2:30remaining
Output of JUnit lifecycle hooks order
Given the following JUnit test class, what is the order of printed output when running all tests?
JUnit
import org.junit.jupiter.api.*; @TestInstance(TestInstance.Lifecycle.PER_CLASS) public class SampleTest { @BeforeAll void beforeAll() { System.out.println("BeforeAll"); } @BeforeEach void beforeEach() { System.out.println("BeforeEach"); } @Test void testOne() { System.out.println("TestOne"); } @Test void testTwo() { System.out.println("TestTwo"); } @AfterEach void afterEach() { System.out.println("AfterEach"); } @AfterAll void afterAll() { System.out.println("AfterAll"); } }
Attempts:
2 left
💡 Hint
Remember the order: BeforeAll once, BeforeEach before each test, AfterEach after each test, AfterAll once.
✗ Incorrect
The lifecycle hooks run in this order: BeforeAll once, then for each test BeforeEach, test, AfterEach, and finally AfterAll once.
❓ assertion
advanced2:00remaining
Correct assertion for resource cleanup in @AfterEach
Which assertion correctly verifies that a resource is closed after each test using @AfterEach in JUnit?
JUnit
private Resource resource;
@BeforeEach
void setup() {
resource = new Resource();
}
@AfterEach
void cleanup() {
resource.close();
}Attempts:
2 left
💡 Hint
Check if the resource reports it is closed after cleanup.
✗ Incorrect
After cleanup, the resource should be closed, so assertTrue(resource.isClosed()) is correct.
🔧 Debug
expert3:00remaining
Identify the problem with lifecycle hooks causing test interference
Consider this JUnit test class where tests sometimes fail due to shared state. What is the main issue causing this?
JUnit
import static org.junit.jupiter.api.Assertions.*; public class SharedStateTest { private static int counter = 0; @BeforeAll static void init() { counter = 0; } @Test void testIncrement() { counter++; assertTrue(counter > 0); } @Test void testReset() { counter = 0; assertEquals(0, counter); } }
Attempts:
2 left
💡 Hint
Think about how static variables behave across tests.
✗ Incorrect
Static variables keep their value across tests, causing shared state and test interference. Each test should have isolated state.