0
0
JUnittesting~20 mins

Custom extensions in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JUnit 5 Custom Extension Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a JUnit 5 custom extension with BeforeEachCallback
What will be printed when this JUnit 5 test class runs with the custom extension?
JUnit
import org.junit.jupiter.api.extension.*;
import org.junit.jupiter.api.*;

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

@ExtendWith(MyExtension.class)
class SampleTest {
    @Test
    void testOne() {
        System.out.println("Running testOne");
    }
    @Test
    void testTwo() {
        System.out.println("Running testTwo");
    }
}
ARunning testOne\nRunning testTwo\nSetup before each test\nSetup before each test
BRunning testOne\nSetup before each test\nRunning testTwo\nSetup before each test
CSetup before each test\nSetup before each test\nRunning testOne\nRunning testTwo
DSetup before each test\nRunning testOne\nSetup before each test\nRunning testTwo
Attempts:
2 left
💡 Hint
Remember that BeforeEachCallback runs before each test method.
assertion
intermediate
2:00remaining
Correct assertion to verify extension invocation count
Given a custom JUnit 5 extension that increments a counter each time beforeEach is called, which assertion correctly verifies that the counter equals 3 after running 3 tests?
JUnit
class CountingExtension implements BeforeEachCallback {
    static int count = 0;
    @Override
    public void beforeEach(ExtensionContext context) {
        count++;
    }
}

// After running 3 tests, check count value
AassertEquals(3, CountingExtension.count);
BassertTrue(CountingExtension.count == 3);
CassertFalse(CountingExtension.count != 3);
DassertEquals(CountingExtension.count, 3);
Attempts:
2 left
💡 Hint
Use the standard JUnit assertion method with expected value first.
🔧 Debug
advanced
2:00remaining
Identify the error in this custom extension implementation
What error will occur when running this JUnit 5 extension code?
JUnit
import org.junit.jupiter.api.extension.*;

public class FaultyExtension implements BeforeAllCallback {
    @Override
    public void beforeAll(ExtensionContext context) {
        System.out.println("Before all tests");
    }

    public void beforeEach(ExtensionContext context) {
        System.out.println("Before each test");
    }
}
ANo error; both methods run correctly
BbeforeEach method is never called because it does not override any interface method
CCompilation error due to missing @Override on beforeEach method
DRuntime error: Method beforeEach causes IllegalStateException
Attempts:
2 left
💡 Hint
Check which interfaces the class implements and which methods are called by JUnit.
framework
advanced
2:00remaining
Which JUnit 5 extension point allows modifying test instance before each test?
You want to create a custom extension that modifies the test class instance before each test method runs. Which extension point should you implement?
ATestInstancePostProcessor
BTestExecutionExceptionHandler
CBeforeAllCallback
DBeforeEachCallback
Attempts:
2 left
💡 Hint
Consider which extension point allows you to access and modify the test instance itself.
🧠 Conceptual
expert
2:00remaining
Effect of registering multiple extensions with conflicting callbacks
If two JUnit 5 extensions both implement BeforeEachCallback and are registered on the same test class, what is the expected behavior during test execution?
AJUnit throws an exception due to conflicting extensions
BOnly the first registered extension's beforeEach method is called
CBoth extensions' beforeEach methods are called in registration order before each test
DOnly the last registered extension's beforeEach method is called
Attempts:
2 left
💡 Hint
Think about how JUnit 5 handles multiple extensions implementing the same callback.