Bird
Raised Fist0
JUnittesting~20 mins

ParameterResolver extension in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
ParameterResolver Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of ParameterResolver supplying a String
What is the output of the following JUnit 5 test when using a ParameterResolver that always provides the string "Hello"?
JUnit
import org.junit.jupiter.api.extension.*;
import org.junit.jupiter.api.*;

class HelloParameterResolver implements ParameterResolver {
    @Override
    public boolean supportsParameter(ParameterContext pc, ExtensionContext ec) {
        return pc.getParameter().getType() == String.class;
    }

    @Override
    public Object resolveParameter(ParameterContext pc, ExtensionContext ec) {
        return "Hello";
    }
}

@ExtendWith(HelloParameterResolver.class)
public class GreetingTest {
    @Test
    void testGreeting(String greeting) {
        System.out.println(greeting);
    }
}
Anull
BTest fails with ParameterResolutionException
CCompilation error
DHello
Attempts:
2 left
💡 Hint
Think about what the ParameterResolver returns for the String parameter.
❓ assertion
intermediate
2:00remaining
Assertion on injected parameter value
Given a ParameterResolver that injects an Integer with value 42, which assertion correctly verifies the injected value in a test method?
JUnit
import org.junit.jupiter.api.extension.*;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;

class IntParameterResolver implements ParameterResolver {
    @Override
    public boolean supportsParameter(ParameterContext pc, ExtensionContext ec) {
        return pc.getParameter().getType() == Integer.class;
    }

    @Override
    public Object resolveParameter(ParameterContext pc, ExtensionContext ec) {
        return 42;
    }
}

@ExtendWith(IntParameterResolver.class)
public class NumberTest {
    @Test
    void testNumber(Integer number) {
        // Which assertion is correct here?
    }
}
AassertSame(42, number);
BassertTrue(number == 42);
CassertEquals(42, number);
DassertNotNull(number);
Attempts:
2 left
💡 Hint
Check which assertion compares values correctly for Integer objects.
🔧 Debug
advanced
2:00remaining
Why does ParameterResolver fail to inject?
Consider this ParameterResolver implementation that tries to inject a Double but the test method parameter is of type double (primitive). Why does the injection fail?
JUnit
import org.junit.jupiter.api.extension.*;
import org.junit.jupiter.api.*;

class DoubleParameterResolver implements ParameterResolver {
    @Override
    public boolean supportsParameter(ParameterContext pc, ExtensionContext ec) {
        return pc.getParameter().getType() == Double.class;
    }

    @Override
    public Object resolveParameter(ParameterContext pc, ExtensionContext ec) {
        return 3.14;
    }
}

@ExtendWith(DoubleParameterResolver.class)
public class PiTest {
    @Test
    void testPi(double value) {
        System.out.println(value);
    }
}
AsupportsParameter returns false because double primitive != Double class
BresolveParameter returns wrong type causing ClassCastException
CTest method parameter must be Double, not double
DParameterResolver interface is not implemented correctly
Attempts:
2 left
💡 Hint
Check the difference between primitive types and wrapper classes in Java.
🧠 Conceptual
advanced
2:00remaining
ParameterResolver lifecycle and caching
Which statement about the lifecycle of a ParameterResolver instance in JUnit 5 is correct?
AParameterResolver instances are created once per test class and reused
BParameterResolver instances are singletons shared across all tests
CJUnit creates a new ParameterResolver instance per parameter injection
DA new ParameterResolver instance is created for each test method invocation
Attempts:
2 left
💡 Hint
Think about how JUnit 5 manages extensions and test instances.
❓ framework
expert
3:00remaining
Custom ParameterResolver with conditional support
You want to create a ParameterResolver that only supports parameters annotated with @InjectValue and of type String. Which implementation of supportsParameter is correct?
JUnit
import org.junit.jupiter.api.extension.*;
import java.lang.reflect.Parameter;

@interface InjectValue {}

class ConditionalParameterResolver implements ParameterResolver {
    @Override
    public boolean supportsParameter(ParameterContext pc, ExtensionContext ec) {
        return pc.isAnnotated(InjectValue.class) && pc.getParameter().getType() == String.class;
    }

    @Override
    public Object resolveParameter(ParameterContext pc, ExtensionContext ec) {
        return "Injected";
    }
}
Areturn pc.getParameter().getAnnotation(InjectValue.class) != null && pc.getParameter().getType().equals(String.class);
Breturn pc.isAnnotated(InjectValue.class) && pc.getParameter().getType() == String.class;
Creturn pc.getParameter().isAnnotationPresent(InjectValue.class) && pc.getParameter().getType() == String.class;
Dreturn pc.isAnnotated(InjectValue.class) && pc.getParameter().getType().equals(String.class);
Attempts:
2 left
💡 Hint
Use ParameterContext methods to check annotation and type.

Practice

(1/5)
1. What is the main purpose of a ParameterResolver in JUnit 5?
easy
A. To define the order of test execution
B. To generate test reports after execution
C. To create mock objects for tests
D. To automatically provide values for test method parameters

Solution

  1. Step 1: Understand ParameterResolver role

    A ParameterResolver is designed to supply values to parameters of test methods automatically, so tests don't need manual setup for those parameters.
  2. Step 2: Compare with other options

    Options A, C, and D relate to other testing features but not to parameter injection.
  3. Final Answer:

    To automatically provide values for test method parameters -> Option D
  4. Quick Check:

    ParameterResolver = automatic parameter injection [OK]
Hint: ParameterResolver injects parameters automatically [OK]
Common Mistakes:
  • Confusing ParameterResolver with test ordering
  • Thinking it creates mocks automatically
  • Assuming it generates reports
2. Which two methods must a class implement to be a valid ParameterResolver in JUnit 5?
easy
A. supportsParameter and resolveParameter
B. beforeEach and afterEach
C. initialize and cleanup
D. createParameter and validateParameter

Solution

  1. Step 1: Recall ParameterResolver interface methods

    The ParameterResolver interface requires implementing supportsParameter to check if a parameter is supported, and resolveParameter to provide the value.
  2. Step 2: Eliminate incorrect method pairs

    Options A, B, and C list methods unrelated to ParameterResolver interface.
  3. Final Answer:

    supportsParameter and resolveParameter -> Option A
  4. Quick Check:

    ParameterResolver methods = supportsParameter + resolveParameter [OK]
Hint: Remember: supportsParameter checks, resolveParameter provides [OK]
Common Mistakes:
  • Confusing lifecycle methods with resolver methods
  • Using incorrect method names
  • Assuming initialization methods are required
3. Given this ParameterResolver implementation, what will be printed when the test runs?
class MyResolver implements ParameterResolver {
  public boolean supportsParameter(ParameterContext pc, ExtensionContext ec) {
    return pc.getParameter().getType() == String.class;
  }
  public Object resolveParameter(ParameterContext pc, ExtensionContext ec) {
    return "InjectedValue";
  }
}

@ExtendWith(MyResolver.class)
class TestClass {
  @Test
  void testMethod(String param) {
    System.out.println(param);
  }
}
medium
A. null
B. InjectedValue
C. Compilation error
D. Test fails with exception

Solution

  1. Step 1: Analyze supportsParameter method

    It returns true only if the parameter type is String, so the test method's String param is supported.
  2. Step 2: Analyze resolveParameter method

    It returns the string "InjectedValue", so this value will be injected into the test method parameter.
  3. Final Answer:

    InjectedValue -> Option B
  4. Quick Check:

    Parameter injection prints InjectedValue [OK]
Hint: Check supportsParameter return true for parameter type [OK]
Common Mistakes:
  • Assuming parameter is null if not supported
  • Expecting compilation error due to interface methods
  • Thinking test fails without explicit parameter value
4. Identify the error in this ParameterResolver implementation:
class FaultyResolver implements ParameterResolver {
  public boolean supportsParameter(ParameterContext pc, ExtensionContext ec) {
    return pc.getParameter().getType() == Integer.class;
  }
  public Object resolveParameter(ParameterContext pc, ExtensionContext ec) {
    return 42;
  }
}

@ExtendWith(FaultyResolver.class)
class TestClass {
  @Test
  void testMethod(int param) {
    System.out.println(param);
  }
}
medium
A. Missing @Override annotations cause runtime error
B. resolveParameter returns wrong type
C. supportsParameter checks for Integer.class but parameter is primitive int
D. Test method parameter must be Integer, not int

Solution

  1. Step 1: Check parameter type in supportsParameter

    The test method parameter is primitive int, but supportsParameter checks for Integer.class, which is the wrapper class. This causes supportsParameter to return false.
  2. Step 2: Understand consequence of mismatch

    Because supportsParameter returns false, resolveParameter is never called, so the parameter is not injected, causing test failure or default value usage.
  3. Final Answer:

    supportsParameter checks for Integer.class but parameter is primitive int -> Option C
  4. Quick Check:

    Primitive vs wrapper mismatch breaks supportsParameter [OK]
Hint: Match primitive and wrapper types carefully in supportsParameter [OK]
Common Mistakes:
  • Ignoring difference between int and Integer
  • Assuming @Override annotations are mandatory
  • Thinking resolveParameter type must differ
5. You want to create a ParameterResolver that injects a unique UUID string into any test method parameter of type String. Which implementation correctly achieves this and ensures a new UUID is generated for each test invocation?
hard
A. In resolveParameter, return UUID.randomUUID().toString() without caching
B. In supportsParameter, cache a UUID and return it for all calls
C. Return a fixed UUID string in resolveParameter for all tests
D. Generate UUID once in constructor and return it in resolveParameter

Solution

  1. Step 1: Understand requirement for unique UUID per test

    Each test invocation should get a new UUID string, so the UUID must be generated fresh every time resolveParameter is called.
  2. Step 2: Evaluate options for UUID generation

    In resolveParameter, return UUID.randomUUID().toString() without caching generates a new UUID string on each call, fulfilling the requirement. Options B, C, and D cache or fix the UUID, so they do not produce unique values per test.
  3. Final Answer:

    In resolveParameter, return UUID.randomUUID().toString() without caching -> Option A
  4. Quick Check:

    Generate UUID fresh in resolveParameter for uniqueness [OK]
Hint: Generate new UUID inside resolveParameter method [OK]
Common Mistakes:
  • Caching UUID and returning same value every time
  • Generating UUID once in constructor
  • Returning fixed string instead of UUID
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