Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to implement a JUnit extension that runs code before each test.
JUnit
public class MyExtension implements BeforeEachCallback { @Override public void [1](ExtensionContext context) throws Exception { System.out.println("Before each test"); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using afterEach instead of beforeEach
Using beforeAll which runs once before all tests
✗ Incorrect
The method to override in BeforeEachCallback is beforeEach, which runs before each test method.
2fill in blank
mediumComplete the code to implement a JUnit extension that runs code after each test.
JUnit
public class MyExtension implements AfterEachCallback { @Override public void [1](ExtensionContext context) throws Exception { System.out.println("After each 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 once after all tests
✗ Incorrect
The method to override in AfterEachCallback is afterEach, which runs after each test method.
3fill in blank
hardFix the error in the method signature to correctly implement BeforeEachCallback.
JUnit
public class MyExtension implements BeforeEachCallback { @Override public void [1] { System.out.println("Before each test"); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the ExtensionContext parameter
Not declaring throws Exception
✗ Incorrect
The beforeEach method must accept ExtensionContext and declare throws Exception to correctly override the interface method.
4fill in blank
hardFill both blanks to implement a JUnit extension that prints messages before and after each test.
JUnit
public class MyExtension implements BeforeEachCallback, AfterEachCallback { @Override public void [1](ExtensionContext context) throws Exception { System.out.println("Starting test"); } @Override public void [2](ExtensionContext context) throws Exception { System.out.println("Finished test"); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using beforeAll or afterAll which run once per test class
Swapping beforeEach and afterEach method names
✗ Incorrect
beforeEach runs before each test, afterEach runs after each test. These are the correct method names for the respective interfaces.
5fill in blank
hardFill all three blanks to create a JUnit extension that logs before each test, after each test, and handles exceptions.
JUnit
public class LoggingExtension implements BeforeEachCallback, AfterEachCallback { @Override public void [1](ExtensionContext context) throws Exception { System.out.println("Log start: " + context.getDisplayName()); } @Override public void [2](ExtensionContext context) throws Exception { System.out.println("Log end: " + context.getDisplayName()); } public void handleException(Throwable [3]) { System.out.println("Exception caught: " + [3].getMessage()); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method names for the interface methods
Using invalid variable names for the exception parameter
✗ Incorrect
The methods beforeEach and afterEach are from the interfaces. The exception parameter name can be any valid identifier; here 'ex' is used.