0
0
JUnittesting~20 mins

Why extensions customize JUnit behavior - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JUnit Extension Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Purpose of JUnit Extensions

Why do developers use extensions to customize JUnit behavior?

ATo add new features like custom test lifecycle events and parameter injection
BTo disable all test execution and only generate reports
CTo change the Java language syntax used in test classes
DTo replace JUnit's core assertion methods with third-party ones
Attempts:
2 left
💡 Hint

Think about how extensions help tests do more than just run assertions.

Predict Output
intermediate
2:00remaining
Output of a JUnit Extension Modifying Test Execution

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

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

class SkipTestExtension implements ExecutionCondition {
    @Override
    public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
        if (context.getDisplayName().contains("skip")) {
            return ConditionEvaluationResult.disabled("Test skipped by extension");
        }
        return ConditionEvaluationResult.enabled("Test enabled");
    }
}

@ExtendWith(SkipTestExtension.class)
class SampleTest {
    @Test
    void testRun() {
        System.out.println("Test is running");
    }

    @Test
    void test_skip() {
        System.out.println("This should not print");
    }
}
ANo output prints because all tests are skipped
BBoth 'Test is running' and 'This should not print' print
COnly 'Test is running' prints; 'test_skip' is skipped
DCompilation error due to missing override annotation
Attempts:
2 left
💡 Hint

Look at how the extension disables tests with 'skip' in their name.

assertion
advanced
2:00remaining
Correct Assertion to Verify Extension Behavior

Which assertion correctly verifies that a JUnit extension has injected a non-null parameter into a test method?

JUnit
@ExtendWith(MyParameterResolver.class)
void testWithParam(String param) {
    // Which assertion to use here?
}
AAssertions.assertNull(param);
BAssertions.assertEquals(null, param);
CAssertions.assertTrue(param.isEmpty());
DAssertions.assertNotNull(param);
Attempts:
2 left
💡 Hint

Parameter injection should provide a valid object, not null.

🔧 Debug
advanced
2:00remaining
Debugging a Failing JUnit Extension

A custom JUnit extension intended to log before each test is not printing logs. What is the most likely cause?

JUnit
public class LoggingExtension implements BeforeTestExecutionCallback {
    @Override
    public void beforeTestExecution(ExtensionContext context) {
        System.out.println("Starting test: " + context.getDisplayName());
    }
}

// Test class missing @ExtendWith(LoggingExtension.class)
AThe test class is missing the @ExtendWith annotation to register the extension
BJUnit 5 does not support BeforeTestExecutionCallback interface
CSystem.out.println does not work inside extensions
DThe extension class must implement AfterTestExecutionCallback instead
Attempts:
2 left
💡 Hint

Check if the extension is properly registered with the test class.

framework
expert
2:00remaining
Extension Point for Custom Test Instance Post-Processing

Which JUnit 5 extension point allows modifying the test instance after it is created but before any tests run?

ABeforeAllCallback
BTestInstancePostProcessor
CParameterResolver
DExecutionCondition
Attempts:
2 left
💡 Hint

Think about the extension that can change the test object itself after creation.