0
0
JUnittesting~10 mins

BeforeEachCallback and AfterEachCallback 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 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'
AbeforeEach
BbeforeAll
CafterEach
DafterAll
Attempts:
3 left
💡 Hint
Common Mistakes
Using afterEach instead of beforeEach
Using beforeAll which runs once before all tests
2fill in blank
medium

Complete 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'
AbeforeEach
BafterEach
CbeforeAll
DafterAll
Attempts:
3 left
💡 Hint
Common Mistakes
Using beforeEach instead of afterEach
Using afterAll which runs once after all tests
3fill in blank
hard

Fix 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'
AafterEach(ExtensionContext context) throws Exception
BbeforeEach()
CbeforeAll(ExtensionContext context)
DbeforeEach(ExtensionContext context) throws Exception
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the ExtensionContext parameter
Not declaring throws Exception
4fill in blank
hard

Fill 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'
AbeforeEach
BafterAll
CafterEach
DbeforeAll
Attempts:
3 left
💡 Hint
Common Mistakes
Using beforeAll or afterAll which run once per test class
Swapping beforeEach and afterEach method names
5fill in blank
hard

Fill 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'
AbeforeEach
BafterEach
Cex
Dexception
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method names for the interface methods
Using invalid variable names for the exception parameter