0
0
JUnittesting~20 mins

Extension model overview 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
2:00remaining
What is the primary purpose of JUnit's Extension Model?

JUnit 5 introduced an extension model. What is its main purpose?

ATo provide a graphical user interface for running tests
BTo replace the need for assertions in test methods
CTo automatically generate test data without user input
DTo allow users to add custom behavior to tests like lifecycle callbacks and parameter resolution
Attempts:
2 left
💡 Hint

Think about how you can customize or extend test execution behavior in JUnit.

Predict Output
intermediate
2:00remaining
Output of a test using a simple JUnit extension

Given the following JUnit 5 extension and test class, what will be printed when the test runs?

JUnit
import org.junit.jupiter.api.extension.*;

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

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(SimpleExtension.class)
public class MyTest {
    @Test
    void testMethod() {
        System.out.println("Test method running");
    }
}
A
Test method running
Before each test
B
Before each test
Test method running
COnly Test method running
DNo output printed
Attempts:
2 left
💡 Hint

Remember that BeforeEachCallback runs before each test method.

assertion
advanced
2:00remaining
Correct assertion to verify extension invocation

You want to verify that a JUnit extension's beforeAll method was called exactly once before all tests. Which assertion correctly checks this?

AassertNull(extension.getBeforeAllCallCount())
BassertTrue(extension.getBeforeAllCallCount() > 1)
CassertEquals(1, extension.getBeforeAllCallCount())
DassertFalse(extension.getBeforeAllCallCount() == 0)
Attempts:
2 left
💡 Hint

Think about how to check the exact number of times a method was called.

🔧 Debug
advanced
2:00remaining
Why does this JUnit extension not run before tests?

Consider this JUnit 5 extension class:

public class MyExtension implements BeforeEachCallback {
    public void beforeAll(ExtensionContext context) {
        System.out.println("Before all tests");
    }

    @Override
    public void beforeEach(ExtensionContext context) {
        System.out.println("Before each test");
    }
}

Why is "Before all tests" never printed?

ABecause beforeAll is not annotated or overridden from an interface, so it is never called by JUnit
BBecause the extension class is missing @ExtendWith annotation
CBecause beforeAll method has wrong parameter type
DBecause beforeEachCallback interface does not support beforeAll method
Attempts:
2 left
💡 Hint

Check which methods JUnit calls automatically in extensions.

framework
expert
2:00remaining
How to implement conditional test execution with JUnit extensions?

You want to skip a test if a certain environment variable is not set. Which JUnit 5 extension interface should you implement to conditionally disable tests?

AExecutionCondition
BTestInstancePostProcessor
CBeforeEachCallback
DParameterResolver
Attempts:
2 left
💡 Hint

Think about how to enable or disable tests before they run.