Complete the code to run setup before each test method.
@BeforeEach void [1]() { System.out.println("Setup before test"); }
The method annotated with @BeforeEach runs before every test. Naming it setup is a common convention.
Complete the code to run teardown after each test method.
@AfterEach void [1]() { System.out.println("Cleanup after test"); }
The method annotated with @AfterEach runs after every test. Naming it tearDown is a common practice to indicate cleanup.
Fix the error in the lifecycle hook annotation to run setup once before all tests.
@[1] static void globalSetup() { System.out.println("Setup once before all tests"); }
@BeforeAll runs once before all tests in the class. It must be used with a static method.
Fill both blanks to run cleanup once after all tests and make the method static.
@[1] static void [2]() { System.out.println("Cleanup once after all tests"); }
@AfterAll runs once after all tests. The method must be static. Naming it tearDownAll clearly shows it cleans up after all tests.
Fill all three blanks to create a test class with setup before each test, teardown after each test, and a test method.
import org.junit.jupiter.api.*; public class SampleTest { @BeforeEach void [1]() { System.out.println("Setup before test"); } @AfterEach void [2]() { System.out.println("Cleanup after test"); } @Test void [3]() { Assertions.assertTrue(true); } }
This test class uses lifecycle hooks to prepare and clean up before and after each test. The test method testAlwaysPasses contains a simple assertion that always passes.