Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to mark a method that runs once after all tests in a JUnit 5 test class.
JUnit
import org.junit.jupiter.api.[1]; public class CalculatorTest { @[1] static void cleanup() { System.out.println("Cleanup after all tests"); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @AfterEach instead of @AfterAll
Not making the method static
✗ Incorrect
The @AfterAll annotation marks a method to run once after all tests in the class have finished.
2fill in blank
mediumComplete the code to ensure the @AfterAll method is static as required by JUnit 5.
JUnit
import org.junit.jupiter.api.AfterAll; public class DatabaseTest { @AfterAll [1] closeConnection() { System.out.println("Closing DB connection"); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting static keyword
Making method non-static with @AfterAll
✗ Incorrect
In JUnit 5, @AfterAll methods must be static unless using a test instance lifecycle that allows non-static.
3fill in blank
hardFix the error in the @AfterAll method declaration to make it valid in JUnit 5.
JUnit
import org.junit.jupiter.api.AfterAll; public class ServiceTest { @AfterAll public [1] cleanup() { System.out.println("Service cleanup"); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-void return types
Returning values from @AfterAll methods
✗ Incorrect
@AfterAll methods must have void return type because they are lifecycle hooks, not test methods.
4fill in blank
hardFill both blanks to correctly define an @AfterAll method that prints a message and is static.
JUnit
import org.junit.jupiter.api.AfterAll; public class NetworkTest { @AfterAll [1] [2] cleanupNetwork() { System.out.println("Network cleanup done"); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting static keyword
Using non-void return type
✗ Incorrect
The method must be static and have void return type to be a valid @AfterAll method.
5fill in blank
hardFill all three blanks to create a valid @AfterAll method with public visibility, static modifier, and void return type.
JUnit
import org.junit.jupiter.api.AfterAll; public class FileTest { @AfterAll [1] [2] [3] cleanupFiles() { System.out.println("Files cleaned up"); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using private or default visibility
Omitting static keyword
Using non-void return type
✗ Incorrect
A valid @AfterAll method is usually public, static, and void to be accessible and run after all tests.