Challenge - 5 Problems
JUnit @AfterEach Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this JUnit test with @AfterEach?
Consider the following JUnit 5 test class. What will be printed to the console when the tests run?
JUnit
import org.junit.jupiter.api.*; public class SampleTest { @BeforeEach void setup() { System.out.println("Setup"); } @Test void testOne() { System.out.println("Test One"); } @Test void testTwo() { System.out.println("Test Two"); } @AfterEach void cleanup() { System.out.println("Cleanup"); } }
Attempts:
2 left
💡 Hint
Remember that @BeforeEach and @AfterEach run before and after each test method.
✗ Incorrect
JUnit runs @BeforeEach before each test and @AfterEach after each test. So for two tests, the sequence is: setup, testOne, cleanup, setup, testTwo, cleanup.
❓ assertion
intermediate2:00remaining
Which assertion correctly verifies @AfterEach method execution?
You want to verify that a resource is closed after each test using @AfterEach. Which assertion in the test class correctly checks this?
JUnit
import org.junit.jupiter.api.*; import static org.junit.jupiter.api.Assertions.*; class ResourceTest { private Resource resource; @BeforeEach void init() { resource = new Resource(); resource.open(); } @Test void testResourceUsage() { assertTrue(resource.isOpen()); } @AfterEach void cleanup() { resource.close(); // Which assertion here verifies cleanup? // ??? } class Resource { private boolean open = false; void open() { open = true; } void close() { open = false; } boolean isOpen() { return open; } } }
Attempts:
2 left
💡 Hint
After cleanup, the resource should be closed, so isOpen() should be false.
✗ Incorrect
The @AfterEach method closes the resource, so after it runs, resource.isOpen() returns false. The assertion assertFalse(resource.isOpen()) correctly verifies this.
🔧 Debug
advanced2:00remaining
Why does the @AfterEach method not run after a test failure?
Given this test class, the @AfterEach method is not executed after a failing test. What is the most likely cause?
JUnit
import org.junit.jupiter.api.*; class FailureTest { @Test void testFail() { Assertions.fail("Failing test"); } @AfterEach void afterEach() { System.out.println("Cleanup after test"); } }
Attempts:
2 left
💡 Hint
Think about JUnit 5 lifecycle guarantees.
✗ Incorrect
JUnit 5 guarantees that @AfterEach runs after every test method, regardless of success or failure. If @AfterEach is not running, the issue is likely in the test setup or environment, not JUnit behavior.
🧠 Conceptual
advanced1:30remaining
What is the main purpose of the @AfterEach method in JUnit?
Choose the best description of the @AfterEach method's role in JUnit testing.
Attempts:
2 left
💡 Hint
Think about when you want to reset or clean after tests.
✗ Incorrect
@AfterEach runs after every test method to clean up or reset things so tests don't affect each other.
❓ framework
expert2:30remaining
How to ensure @AfterEach runs even if a test times out or is interrupted?
In JUnit 5, you want to guarantee that cleanup code in @AfterEach runs even if a test times out or is interrupted. Which approach ensures this behavior?
Attempts:
2 left
💡 Hint
Consider how preemptive timeouts affect test lifecycle methods.
✗ Incorrect
When using assertTimeoutPreemptively, the test thread is interrupted and @AfterEach may not run. To guarantee cleanup, use try-finally inside the test method itself.