Bird
Raised Fist0
JUnittesting~20 mins

Why extensions customize JUnit behavior - Challenge Your Understanding

Choose your learning style10 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.

Practice

(1/5)
1. What is the main reason to use extensions in JUnit tests?
easy
A. To add extra behavior without changing the test code
B. To write test cases faster
C. To replace the JUnit framework
D. To create user interfaces for tests

Solution

  1. Step 1: Understand the purpose of extensions

    Extensions allow adding extra behavior like setup or cleanup without modifying test methods.
  2. Step 2: Compare options with this purpose

    Only To add extra behavior without changing the test code matches this purpose; others describe unrelated goals.
  3. Final Answer:

    To add extra behavior without changing the test code -> Option A
  4. Quick Check:

    Extensions add behavior = D [OK]
Hint: Extensions add behavior without changing tests [OK]
Common Mistakes:
  • Thinking extensions speed up writing tests
  • Confusing extensions with replacing JUnit
  • Assuming extensions create UI
2. Which annotation is used to apply an extension to a JUnit test class or method?
easy
A. @TestExtension
B. @UseExtension
C. @ExtendWith
D. @EnableExtension

Solution

  1. Step 1: Recall the correct annotation for extensions

    The standard JUnit annotation to apply extensions is @ExtendWith.
  2. Step 2: Verify other options

    Other options are not valid JUnit annotations for extensions.
  3. Final Answer:

    @ExtendWith -> Option C
  4. Quick Check:

    @ExtendWith = C [OK]
Hint: Remember '@ExtendWith' applies extensions [OK]
Common Mistakes:
  • Using @TestExtension which does not exist
  • Confusing with @UseExtension or @EnableExtension
  • Forgetting the '@' symbol
3. Given this code snippet, what will be printed when the test runs?
@ExtendWith(MyExtension.class)
class MyTest {
  @Test
  void testMethod() {
    System.out.println("Test running");
  }
}

Assuming MyExtension prints "Before test" before the test and "After test" after the test.
medium
A. Test running\nBefore test\nAfter test
B. Before test\nTest running\nAfter test
C. After test\nTest running\nBefore test
D. Test running only

Solution

  1. Step 1: Understand extension behavior

    MyExtension runs code before and after the test method, printing "Before test" and "After test" respectively.
  2. Step 2: Order of output

    Output order is: before test message, test method message, after test message.
  3. Final Answer:

    Before test\nTest running\nAfter test -> Option B
  4. Quick Check:

    Extension runs before/after test = A [OK]
Hint: Extensions run code before and after tests [OK]
Common Mistakes:
  • Assuming extension runs after test only
  • Mixing order of printed lines
  • Ignoring extension output
4. Identify the error in this JUnit test using an extension:
@ExtendWith(MyExtension.class)
class TestClass {
  @Test
  void test() {
    System.out.println("Running test");
  }

  void helper() {
    System.out.println("Helper method");
  }
}

Why might helper() not be called during test execution?
medium
A. Extension disables helper methods
B. Helper method must be private
C. Helper method must be static
D. Helper method is not annotated with @Test

Solution

  1. Step 1: Check method annotations

    Only methods annotated with @Test run as tests; helper() lacks this annotation.
  2. Step 2: Understand extension role

    Extensions do not disable helper methods; they only affect test lifecycle.
  3. Final Answer:

    Helper method is not annotated with @Test -> Option D
  4. Quick Check:

    Missing @Test means no run = B [OK]
Hint: Only @Test methods run automatically [OK]
Common Mistakes:
  • Thinking extensions disable helper methods
  • Believing helper must be static or private
  • Assuming all methods run as tests
5. You want to create a JUnit extension that changes test data before each test runs. Which approach best uses extensions to achieve this?
hard
A. Implement BeforeEachCallback and modify test data in beforeEach()
B. Override the test method and change data inside it
C. Use @Test annotation with parameters to change data
D. Create a separate test class for each data variation

Solution

  1. Step 1: Identify extension interface for pre-test actions

    The BeforeEachCallback interface allows running code before each test method.
  2. Step 2: Apply this to modify test data

    Modifying test data in beforeEach() method is the correct way to customize data before tests.
  3. Final Answer:

    Implement BeforeEachCallback and modify test data in beforeEach() -> Option A
  4. Quick Check:

    BeforeEachCallback modifies data before tests = A [OK]
Hint: Use BeforeEachCallback to run code before tests [OK]
Common Mistakes:
  • Trying to override test methods directly
  • Misusing @Test parameters for data changes
  • Creating many classes instead of using extensions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard