0
0
JUnittesting~20 mins

@AfterAll method in JUnit - Practice Problems & Coding Challenges

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

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

    @BeforeAll
    void beforeAll() {
        System.out.println("BeforeAll");
    }

    @Test
    void testOne() {
        System.out.println("TestOne");
    }

    @Test
    void testTwo() {
        System.out.println("TestTwo");
    }

    @AfterAll
    void afterAll() {
        System.out.println("AfterAll");
    }
}
ABeforeAll\nAfterAll\nTestOne\nTestTwo
BBeforeAll\nTestOne\nTestTwo\nAfterAll
CTestOne\nTestTwo\nBeforeAll\nAfterAll
DTestOne\nBeforeAll\nTestTwo\nAfterAll
Attempts:
2 left
💡 Hint
Remember that @BeforeAll runs once before any tests, and @AfterAll runs once after all tests.
assertion
intermediate
1:30remaining
Correct assertion to verify @AfterAll cleanup
You want to verify that a resource is cleaned up after all tests run using @AfterAll. Which assertion correctly checks that the resource is null after cleanup?
JUnit
public class ResourceTest {
    private static Resource resource;

    @BeforeAll
    static void setup() {
        resource = new Resource();
    }

    @AfterAll
    static void cleanup() {
        resource = null;
    }

    @Test
    void testResourceNotNull() {
        Assertions.assertNotNull(resource);
    }

    // Which assertion below correctly verifies cleanup?
}
AAssertions.assertTrue(resource != null);
BAssertions.assertNotNull(resource);
CAssertions.assertNull(resource);
DAssertions.assertFalse(resource == null);
Attempts:
2 left
💡 Hint
After cleanup, the resource should be null.
🔧 Debug
advanced
2:00remaining
Why does @AfterAll method cause a failure?
Given this JUnit 5 test class, why does the test suite fail with an exception related to the @AfterAll method?
JUnit
import org.junit.jupiter.api.*;

public class FailureTest {

    @AfterAll
    static void cleanup() {
        System.out.println("Cleaning up");
    }

    @Test
    void testSomething() {
        Assertions.assertTrue(true);
    }
}
A@AfterAll method must return a boolean
B@AfterAll method must be private
C@AfterAll method cannot print to console
D@AfterAll method must be static unless using @TestInstance(PER_CLASS)
Attempts:
2 left
💡 Hint
Check the method modifiers and class annotations.
🧠 Conceptual
advanced
1:30remaining
Purpose of @AfterAll in test lifecycle
What is the main purpose of the @AfterAll method in a JUnit 5 test class?
ATo run code once after all test methods have executed, typically for cleanup
BTo run code before each test method to set up test data
CTo run code after each test method to reset mocks
DTo run code only if a test method fails
Attempts:
2 left
💡 Hint
Think about when @AfterAll runs compared to individual tests.
framework
expert
2:30remaining
Behavior of @AfterAll with parallel test execution
In a JUnit 5 test suite configured to run tests in parallel, how does the @AfterAll method behave?
A@AfterAll runs once after all parallel tests in the class complete, regardless of thread
B@AfterAll runs after each test thread finishes, multiple times
C@AfterAll runs before any parallel tests start
D@AfterAll runs only if tests run sequentially, skipped in parallel mode
Attempts:
2 left
💡 Hint
Consider the lifecycle scope of @AfterAll in parallel execution.