Bird
Raised Fist0
JUnittesting~10 mins

@AfterEach method in JUnit - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to mark the method that runs after each test.

JUnit
public class CalculatorTest {

    @Test
    public void testAdd() {
        // test code here
    }

    [1]
    public void cleanup() {
        System.out.println("Cleaning up after test");
    }
}
Drag options to blanks, or click blank then click option'
A@AfterEach
B@BeforeEach
C@BeforeAll
D@AfterAll
Attempts:
3 left
💡 Hint
Common Mistakes
Using @BeforeEach instead of @AfterEach
Using @AfterAll which runs only once after all tests
2fill in blank
medium

Complete the code to ensure the cleanup method runs after each test in JUnit 5.

JUnit
import org.junit.jupiter.api.[1];

public class SampleTest {

    @Test
    void testSomething() {
        // test logic
    }

    @AfterEach
    void cleanup() {
        System.out.println("Cleanup after test");
    }
}
Drag options to blanks, or click blank then click option'
ABeforeEach
BAfterEach
CTest
DBeforeAll
Attempts:
3 left
💡 Hint
Common Mistakes
Importing @BeforeEach instead of @AfterEach
Forgetting to import the annotation
3fill in blank
hard

Fix the error in the code to properly run cleanup after each test.

JUnit
public class TestExample {

    @Test
    public void testOne() {
        // test code
    }

    [1]
    public void cleanup() {
        System.out.println("Cleanup done");
    }
}
Drag options to blanks, or click blank then click option'
A@AfterAll
B@BeforeEach
C@AfterEach
D@Test
Attempts:
3 left
💡 Hint
Common Mistakes
Using @AfterAll which runs only once after all tests
Using @BeforeEach which runs before each test
4fill in blank
hard

Fill both blanks to create a test class with setup and cleanup methods running before and after each test.

JUnit
import org.junit.jupiter.api.[1];
import org.junit.jupiter.api.[2];

public class MyTest {

    @BeforeEach
    void setup() {
        System.out.println("Setup before test");
    }

    @AfterEach
    void cleanup() {
        System.out.println("Cleanup after test");
    }

    @Test
    void testMethod() {
        System.out.println("Running test");
    }
}
Drag options to blanks, or click blank then click option'
ABeforeEach
BAfterEach
CTest
DBeforeAll
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up @BeforeEach and @AfterEach
Forgetting to import one of the annotations
5fill in blank
hard

Fill all three blanks to create a test class with proper imports and an @AfterEach cleanup method.

JUnit
import org.junit.jupiter.api.[1];
import org.junit.jupiter.api.[2];
import org.junit.jupiter.api.[3];

public class CleanupTest {

    @BeforeEach
    void setup() {
        System.out.println("Setup before test");
    }

    @Test
    void testAction() {
        System.out.println("Test running");
    }

    @AfterEach
    void cleanup() {
        System.out.println("Cleanup after test");
    }
}
Drag options to blanks, or click blank then click option'
ATest
BAfterEach
CBeforeEach
DBeforeAll
Attempts:
3 left
💡 Hint
Common Mistakes
Missing one or more imports
Confusing @BeforeEach with @BeforeAll

Practice

(1/5)
1. What is the main purpose of a method annotated with @AfterEach in JUnit?
easy
A. To define a test method itself
B. To run code before any test methods start
C. To run cleanup code after each test method executes
D. To skip a test method during execution

Solution

  1. Step 1: Understand the role of @AfterEach

    The @AfterEach annotation marks methods that run after every test method to clean up or reset state.
  2. Step 2: Compare options with this role

    Only To run cleanup code after each test method executes correctly describes this cleanup role after each test method.
  3. Final Answer:

    To run cleanup code after each test method executes -> Option C
  4. Quick Check:

    @AfterEach = cleanup after tests [OK]
Hint: Remember: @AfterEach runs after every test method [OK]
Common Mistakes:
  • Confusing @AfterEach with @BeforeEach
  • Thinking @AfterEach defines test methods
  • Assuming @AfterEach skips tests
2. Which of the following is the correct method signature for a method annotated with @AfterEach in JUnit?
easy
A. public int cleanup()
B. public void cleanup()
C. public void cleanup(String param)
D. public static void cleanup()

Solution

  1. Step 1: Recall method requirements for @AfterEach

    Methods with @AfterEach must be void, no-argument, and instance methods (not static).
  2. Step 2: Check each option

    public void cleanup() matches: public, void return, no parameters, instance method. Options B and C have wrong return type or parameters. public static void cleanup() is static, which is not allowed.
  3. Final Answer:

    public void cleanup() -> Option B
  4. Quick Check:

    @AfterEach method = void, no-args, non-static [OK]
Hint: @AfterEach methods are void, no parameters, non-static [OK]
Common Mistakes:
  • Adding parameters to @AfterEach methods
  • Making @AfterEach methods static
  • Using non-void return types
3. Consider the following JUnit test class snippet:
class ExampleTest {
  int count = 0;

  @AfterEach
  void reset() {
    count = 0;
  }

  @Test
  void testIncrement() {
    count++;
    assertEquals(1, count);
  }

  @Test
  void testDecrement() {
    count--;
    assertEquals(-1, count);
  }
}

What will be the result when running both tests?
medium
A. Both tests pass because count resets after each test
B. Both tests fail because count is shared and not reset
C. testIncrement passes, testDecrement fails
D. testIncrement fails, testDecrement passes

Solution

  1. Step 1: Understand @AfterEach effect on count

    The reset() method sets count to 0 after each test method runs, so each test starts fresh.
  2. Step 2: Analyze each test

    In testIncrement(), count increments from 0 to 1, assertion matches. In testDecrement(), count decrements from 0 to -1, assertion matches. Both pass.
  3. Final Answer:

    Both tests pass because count resets after each test -> Option A
  4. Quick Check:

    @AfterEach resets state = tests independent [OK]
Hint: @AfterEach resets variables so tests don't affect each other [OK]
Common Mistakes:
  • Assuming count keeps value between tests
  • Forgetting @AfterEach runs after each test
  • Confusing @AfterEach with @BeforeEach
4. Identify the error in this JUnit test class:
class SampleTest {

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

  @Test
  void testSomething() {
    assertTrue(true);
  }
}
medium
A. The @AfterEach method should not be static
B. The @AfterEach method must return boolean
C. The @Test method must be static
D. The @AfterEach method must have parameters

Solution

  1. Step 1: Check @AfterEach method signature rules

    @AfterEach methods must be instance methods (non-static), void, and no-argument.
  2. Step 2: Identify the error in the code

    The method cleanup() is declared static, which violates the rule.
  3. Final Answer:

    The @AfterEach method should not be static -> Option A
  4. Quick Check:

    @AfterEach methods cannot be static [OK]
Hint: @AfterEach methods must be instance, not static [OK]
Common Mistakes:
  • Making @AfterEach methods static
  • Adding parameters to @AfterEach methods
  • Changing return type from void
5. You have a test class where each test opens a database connection. You want to ensure the connection closes after each test to avoid resource leaks. Which is the best way to do this using @AfterEach?
hard
A. Do not close the connection; let JVM handle it
B. Call connection.close() at the start of each test method
C. Use @BeforeEach to close the connection before tests
D. Annotate a method that calls connection.close() with @AfterEach

Solution

  1. Step 1: Understand resource cleanup best practice

    Resources like database connections should be closed after each test to avoid leaks and interference.
  2. Step 2: Apply @AfterEach for cleanup

    Using a method annotated with @AfterEach to close the connection ensures it happens after every test, keeping tests independent and safe.
  3. Final Answer:

    Annotate a method that calls connection.close() with @AfterEach -> Option D
  4. Quick Check:

    @AfterEach = cleanup resources after tests [OK]
Hint: Use @AfterEach to close resources after each test [OK]
Common Mistakes:
  • Closing resources before tests instead of after
  • Not closing resources causing leaks
  • Relying on JVM to close resources automatically
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