0
0
JUnittesting~20 mins

BeforeEachCallback and AfterEachCallback in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JUnit Extension Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Purpose of BeforeEachCallback in JUnit 5
What is the main purpose of implementing the BeforeEachCallback interface in a JUnit 5 extension?
ATo execute code only if a test fails
BTo execute code once before all tests in a test class run
CTo execute code after each test method runs
DTo execute code before each individual test method runs
Attempts:
2 left
💡 Hint
Think about when you want to prepare the test environment for every test method.
🧠 Conceptual
intermediate
1:30remaining
Difference between BeforeEachCallback and AfterEachCallback
Which statement correctly describes the difference between BeforeEachCallback and AfterEachCallback in JUnit 5?
ABeforeEachCallback runs before each test; AfterEachCallback runs after each test
BBeforeEachCallback runs after each test; AfterEachCallback runs before each test
CBoth run before each test but in different threads
DBoth run after all tests have finished
Attempts:
2 left
💡 Hint
Think about setup and cleanup around each test.
Predict Output
advanced
2:00remaining
Output of JUnit Extension with BeforeEachCallback and AfterEachCallback
Given the following JUnit 5 extension code, what will be printed to the console when running a test method using this extension?
JUnit
import org.junit.jupiter.api.extension.*;

public class MyExtension implements BeforeEachCallback, AfterEachCallback {
    @Override
    public void beforeEach(ExtensionContext context) {
        System.out.println("Setup before test");
    }

    @Override
    public void afterEach(ExtensionContext context) {
        System.out.println("Cleanup after test");
    }
}

// Test class uses @ExtendWith(MyExtension.class) and has one test method that prints "Running test"
ASetup before test\nRunning test\nCleanup after test
BCleanup after test\nSetup before test\nRunning test
CSetup before test\nCleanup after test\nRunning test
DRunning test\nSetup before test\nCleanup after test
Attempts:
2 left
💡 Hint
Remember the order: beforeEach runs before the test, afterEach runs after.
🔧 Debug
advanced
2:00remaining
Identify the error in this AfterEachCallback implementation
What error will occur when running this JUnit 5 extension code implementing AfterEachCallback?
JUnit
import org.junit.jupiter.api.extension.*;

public class FaultyExtension implements AfterEachCallback {
    @Override
    public void afterEach() {
        System.out.println("Cleaning up");
    }
}
ANo error, code runs fine
BRuntime NullPointerException
CCompilation error: method afterEach() does not override or implement a method from a supertype
DTest fails with IllegalStateException
Attempts:
2 left
💡 Hint
Check the method signature required by AfterEachCallback interface.
framework
expert
2:30remaining
Correct usage of BeforeEachCallback and AfterEachCallback in a JUnit 5 extension
Which code snippet correctly implements both BeforeEachCallback and AfterEachCallback interfaces in a JUnit 5 extension to print messages before and after each test method?
A
public class Ext implements BeforeEachCallback, AfterEachCallback {
    public void beforeEach() {
        System.out.println("Before");
    }
    public void afterEach() {
        System.out.println("After");
    }
}
B
public class Ext implements BeforeEachCallback, AfterEachCallback {
    @Override
    public void beforeEach(ExtensionContext context) {
        System.out.println("Before");
    }
    @Override
    public void afterEach(ExtensionContext context) {
        System.out.println("After");
    }
}
C
public class Ext implements BeforeEachCallback, AfterEachCallback {
    @Override
    public void beforeEach() {
        System.out.println("Before");
    }
    @Override
    public void afterEach() {
        System.out.println("After");
    }
}
D
public class Ext implements BeforeEachCallback, AfterEachCallback {
    public void beforeEach(ExtensionContext context) {
        System.out.println("Before");
    }
    public void afterEach(ExtensionContext context) {
        System.out.println("After");
    }
}
Attempts:
2 left
💡 Hint
Check method signatures and use of @Override annotations.