Bird
Raised Fist0
JUnittesting~20 mins

@AfterAll method in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of the @AfterAll annotation in JUnit 5?
easy
A. To run a method before each test method
B. To run a method once after all tests in the class have finished
C. To run a method after each test method
D. To run a method before all tests in the class start

Solution

  1. Step 1: Understand the role of @AfterAll

    The @AfterAll annotation is designed to mark a method that runs once after all test methods in the class have completed.
  2. Step 2: Compare with other lifecycle annotations

    @BeforeEach and @AfterEach run before and after each test method respectively, not once after all tests.
  3. Final Answer:

    To run a method once after all tests in the class have finished -> Option B
  4. Quick Check:

    @AfterAll runs once after all tests [OK]
Hint: Remember: AfterAll runs once after all tests finish [OK]
Common Mistakes:
  • Confusing @AfterAll with @AfterEach
  • Thinking it runs before tests
  • Assuming it runs multiple times
2. Which of the following is the correct way to declare a method with @AfterAll in JUnit 5?
easy
A. public void cleanup() { ... }
B. private static void cleanup() { ... }
C. public static void cleanup() { ... }
D. private void cleanup() { ... }

Solution

  1. Step 1: Recall method requirements for @AfterAll

    In JUnit 5, methods annotated with @AfterAll must be static and usually public to be accessible.
  2. Step 2: Check each option

    public void cleanup() { ... } is non-static, so invalid. private static void cleanup() { ... } is private, so invalid. public static void cleanup() { ... } is public static, which is correct. private void cleanup() { ... } is private and non-static, invalid.
  3. Final Answer:

    public static void cleanup() { ... } -> Option C
  4. Quick Check:

    @AfterAll method must be static and public [OK]
Hint: Always make @AfterAll methods static and public [OK]
Common Mistakes:
  • Forgetting to make the method static
  • Using private access modifier
  • Omitting the static keyword
3. Consider the following JUnit 5 test class snippet:
import org.junit.jupiter.api.*;

public class TestExample {
    static int count = 0;

    @Test
    void testOne() {
        count++;
    }

    @Test
    void testTwo() {
        count++;
    }

    @AfterAll
    public static void afterAll() {
        System.out.println("Count: " + count);
    }
}

What will be printed when the tests run?
medium
A. Count: 2
B. Count: 1
C. Count: 0
D. Compilation error

Solution

  1. Step 1: Analyze test method executions

    Both testOne() and testTwo() increment the static variable count by 1 each. So after both tests run, count will be 2.
  2. Step 2: Understand @AfterAll output

    The afterAll() method runs once after all tests and prints the current value of count, which is 2.
  3. Final Answer:

    Count: 2 -> Option A
  4. Quick Check:

    Both tests increment count, printed after all tests [OK]
Hint: Static count increments twice, printed once after all tests [OK]
Common Mistakes:
  • Assuming count resets between tests
  • Thinking @AfterAll runs before tests
  • Confusing instance and static variables
4. Given this JUnit 5 test class:
import org.junit.jupiter.api.*;

public class SampleTest {

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

    @Test
    void test() {
        Assertions.assertTrue(true);
    }
}

What is the problem with this code?
medium
A. The @AfterAll method must be static
B. The @AfterAll method must return a value
C. The @AfterAll method cannot print output
D. The @Test method must be static

Solution

  1. Step 1: Check @AfterAll method signature

    In JUnit 5, @AfterAll methods must be static unless the test class is annotated with @TestInstance(Lifecycle.PER_CLASS). Here, the method is non-static and no such annotation is present.
  2. Step 2: Validate other options

    The method does not need to return a value, printing is allowed, and @Test methods do not need to be static.
  3. Final Answer:

    The @AfterAll method must be static -> Option A
  4. Quick Check:

    @AfterAll requires static method unless PER_CLASS used [OK]
Hint: Check if @AfterAll method is static without PER_CLASS annotation [OK]
Common Mistakes:
  • Forgetting static requirement for @AfterAll
  • Thinking @AfterAll must return a value
  • Confusing @Test method static rules
5. You want to release a shared database connection after all tests in your JUnit 5 class run. Which of the following is the best way to do this using @AfterAll?
hard
A. Declare a non-static @AfterAll method that closes the connection
B. Use @BeforeAll to close the connection before tests
C. Close the connection inside each @Test method
D. Declare a static @AfterAll method that closes the connection

Solution

  1. Step 1: Understand resource cleanup timing

    Shared resources like a database connection should be closed once after all tests finish, not before or during each test.
  2. Step 2: Apply @AfterAll rules

    The cleanup method must be static to run after all tests. Closing the connection in a static @AfterAll method ensures proper cleanup.
  3. Final Answer:

    Declare a static @AfterAll method that closes the connection -> Option D
  4. Quick Check:

    Static @AfterAll method cleans shared resources after tests [OK]
Hint: Use static @AfterAll to clean shared resources once after tests [OK]
Common Mistakes:
  • Making @AfterAll method non-static
  • Closing resources before tests start
  • Closing resources inside each test
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard