0
0
JUnittesting~10 mins

@AfterEach method in JUnit - Interactive Code Practice

Choose your learning style9 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