Bird
Raised Fist0
JUnittesting~20 mins

Extension model overview in JUnit - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. What is the main purpose of the JUnit extension model?
easy
A. To add extra behavior to tests without changing the test code
B. To write test cases faster using templates
C. To automatically generate test data
D. To replace assertions with custom logic

Solution

  1. Step 1: Understand the extension model concept

    The extension model allows adding extra behavior to tests without modifying the original test code.
  2. Step 2: Compare options with the concept

    Only To add extra behavior to tests without changing the test code correctly describes this purpose. Other options describe unrelated features.
  3. Final Answer:

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

    Extension model purpose = add behavior without code change [OK]
Hint: Extension model = add behavior without changing tests [OK]
Common Mistakes:
  • Confusing extension model with test data generation
  • Thinking it replaces assertions
  • Assuming it speeds up test writing
2. Which annotation is used to apply an extension to a JUnit test class?
easy
A. @ExtendWith
B. @TestExtension
C. @UseExtension
D. @EnableExtension

Solution

  1. Step 1: Recall the correct annotation for extensions

    JUnit uses @ExtendWith to apply extensions to test classes or methods.
  2. Step 2: Verify other options

    Options B, C, and D are not valid JUnit annotations for extensions.
  3. Final Answer:

    @ExtendWith -> Option A
  4. Quick Check:

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

class MyExtension implements BeforeTestExecutionCallback {
  public void beforeTestExecution(ExtensionContext context) {
    System.out.println("Before test");
  }
}
medium
A. Test running Before test
B. Before test Test running
C. Test running
D. Before test

Solution

  1. Step 1: Understand the extension callback

    The BeforeTestExecutionCallback runs its method before the test method executes, printing "Before test" first.
  2. Step 2: Order of print statements

    First "Before test" prints, then the test method prints "Test running".
  3. Final Answer:

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

    BeforeTestExecutionCallback runs before test method [OK]
Hint: BeforeTestExecutionCallback runs before test method [OK]
Common Mistakes:
  • Assuming test method prints first
  • Confusing callback timing
  • Ignoring extension execution order
4. Identify the error in this extension implementation:
public class MyExtension implements BeforeTestExecutionCallback {
  public void beforeTestExecution() {
    System.out.println("Before test");
  }
}
medium
A. Method beforeTestExecution should return boolean
B. Method name should be beforeTestStart
C. Class must extend AbstractExtension instead of implementing interface
D. Missing parameter ExtensionContext in beforeTestExecution method

Solution

  1. Step 1: Check method signature for interface implementation

    The interface BeforeTestExecutionCallback requires beforeTestExecution(ExtensionContext context) method with one parameter.
  2. Step 2: Identify missing parameter

    The method in the code has no parameters, so it does not correctly implement the interface.
  3. Final Answer:

    Missing parameter ExtensionContext in beforeTestExecution method -> Option D
  4. Quick Check:

    Interface method requires ExtensionContext parameter [OK]
Hint: Check method parameters match interface exactly [OK]
Common Mistakes:
  • Omitting required method parameters
  • Changing method return type incorrectly
  • Using wrong method names
5. You want to create a JUnit extension that logs a message before and after each test method runs. Which combination of interfaces should your extension implement?
hard
A. BeforeTestExecutionCallback and AfterTestExecutionCallback
B. BeforeAllCallback and AfterAllCallback
C. BeforeEachCallback and AfterEachCallback
D. TestExecutionExceptionHandler and BeforeEachCallback

Solution

  1. Step 1: Understand callback interfaces for test methods

    BeforeEachCallback runs before each test method, and AfterEachCallback runs after each test method.
  2. Step 2: Match requirement to interfaces

    To log before and after each test method, implement both BeforeEachCallback and AfterEachCallback.
  3. Final Answer:

    BeforeEachCallback and AfterEachCallback -> Option C
  4. Quick Check:

    Before/After each test = BeforeEachCallback + AfterEachCallback [OK]
Hint: Before/After each test = BeforeEachCallback + AfterEachCallback [OK]
Common Mistakes:
  • Using BeforeAllCallback which runs once per class
  • Confusing AfterTestExecutionCallback with AfterEachCallback
  • Choosing exception handler interfaces incorrectly
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