VOOZH about

URL: https://www.geeksforgeeks.org/advance-java/resolve-parameterresolutionexception-in-junit-5/

⇱ How to Resolve the ParameterResolutionException in JUnit 5? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Resolve the ParameterResolutionException in JUnit 5?

Last Updated : 23 Jul, 2025

In JUnit 5, the ParameterResolutionException occurs when JUnit fails to inject a dependency on a test method or constructor. JUnit 5 introduces the concept of dependency injection in test methods that allow services or mock objects to be injected directly into test classes. If the required settings do not exist, this can cause a ParameterResolutionException. In this article, we will explore how to resolve ParameterResolutionException in JUnit 5 using a hands-on example.

Prerequisites:

  • Basic knowledge of Java and the JUnit framework.
  • Maven for dependency management.
  • JDK and IntelliJ IDEA installed in your system.

Parameter Resolution in JUnit 5

In JUnit 5, testing with dependency injection allows you to provide the necessary dependencies directly into test methods or constructors. This feature makes it easier to mock dependencies using tools like Mockito without having to write boilerplate code for setting up mocks. However, to fully utilize this feature and avoid common errors like the ParameterResolutionException, it is important to understand how JUnit 5 handles parameter resolution internally.

How Does It Work?

JUnit 5 introduces the Jupiter platform, which allows you to extend its functionality using extensions. One such extension is the Parameter Resolver, which handles the process of injecting dependencies into test methods. When a test method declares a parameter, JUnit attempts to find a registered ParameterResolver that can provide the required parameter.

The ParameterResolver does the following:

  • It checks if it can resolve the specific parameter type for the given test method.
  • If possible, it provides an instance of the required parameter during test execution.

If JUnit fails to find a suitable ParameterResolver for the parameter type, it throws a ParameterResolutionException. This often happens when using mock objects in JUnit tests without registering the required Mockito extension.

MockitoExtension in Focus

The MockitoExtension is a powerful JUnit 5 extension that:

  • Automatically creates instances of fields annotated with @Mock.
  • Injects these mock instances into fields or constructors annotated with @InjectMocks.
  • Simplifies test setup, making test classes more readable and less cluttered with manual mock initialization.

Example of Using the MockitoExtension with JUnit 5:

@ExtendWith(MockitoExtension.class)
public class UserServiceTest {
@Mock
private UserRepository userRepository; // Creates a mock instance of UserRepository

@InjectMocks
private UserService userService; // Injects the mock into UserService

@Test
void testGetUserDetails() {
String userId = "user123";
when(userRepository.findUserById(userId)).thenReturn("John Doe");

String result = userService.getUserDetails(userId);

assertEquals("John Doe", result);
}
}

In the above example:

  • @ExtendWith(MockitoExtension.class) registers the MockitoExtension, enabling JUnit to recognize and inject mock objects.
  • @Mock tells Mockito to create a mock instance of UserRepository.
  • @InjectMocks instructs Mockito to inject the UserRepository mock into the UserService instance.

The MockitoExtension takes care of the parameter resolution process, ensuring that UserRepository is properly mocked and injected into UserService before the test method runs. This eliminates the need for manual setup and prevents ParameterResolutionException.

Implementation to Solve ParameterResolutionException in JUnit 5

Let's create a simple project demonstrating how to properly set up a test with dependency injection in JUnit 5.

Step 1: Create a Maven Project

Create a new Maven project in IntelliJ IDEA. Choose the following options:

  • Name: parameter-resolution-example
  • Build System: Maven

Click on the Create button.

👁 Project Metadata

Project Structure

Once project creation done, set the folder structure as shown in the below image:

👁 Project Folder Structure

Step 2: Add Dependencies in pom.xml

Open the pom.xml file and include the below JUnit 5 and Mockito dependencies in the project.

Step 3: Create the UserRepository Interface

Step 4: Create the UserService Class

Step 5: Main Class

Step 6: Write the Test Class

In this step, we will create a test class and will use the @ExtendWith annotation to enable dependency injection in JUnit 5.

UserServiceTest.java:

Explanation:

  • @ExtendWith(MockitoExtension.class) annotation enables the use of Mockito in JUnit 5, allowing for the parameter resolution of mocks.
  • @Mock creates the mock instance of UserRepository.
  • @InjectMocks injects the userRepository mock into userService.
  • when(...).thenReturn(...) sets up mock behavior for findUserById method.

Step 7: Run the Application

Once the project is completed, we will run the application and it will display the below output:

👁 Output

Step 8: Running the Tests

We will now test the test suites using the following maven command:

mvn test

Output:

The expected output in the terminal will indicate that the tests passed:

👁 Test Output
Comment
Article Tags:
Article Tags:

Explore