Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @BeforeEach instead of @AfterEach
Using @AfterAll which runs only once after all tests
✗ Incorrect
The @AfterEach annotation marks a method to be run after each test method in the class.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing @BeforeEach instead of @AfterEach
Forgetting to import the annotation
✗ Incorrect
The import for @AfterEach is org.junit.jupiter.api.AfterEach to mark methods that run after each test.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @AfterAll which runs only once after all tests
Using @BeforeEach which runs before each test
✗ Incorrect
The method should be annotated with @AfterEach to run after each test method.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up @BeforeEach and @AfterEach
Forgetting to import one of the annotations
✗ Incorrect
The imports must include @BeforeEach and @AfterEach to mark setup and cleanup methods.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Missing one or more imports
Confusing @BeforeEach with @BeforeAll
✗ Incorrect
The imports must include @Test, @AfterEach, and @BeforeEach for a complete test class with setup and cleanup.