The error message you’re encountering, “No ParameterResolver registered for parameter x” is due to the incorrect usage of annotations in the JUnit 5 test.
In JUnit 5,
@ParameterizedTest
is used to indicate that the annotated method is a parameterized test. However, it seems like in your code snippet, the
@ParameterizedTest
annotation is mistakenly combined with
@Test
, causing confusion in the framework.
Here’s a step-by-step tutorial on how to fix this issue:
Root Cause:
Firstly, the error stems from mixing
@Test
and
@ParameterizedTest
annotations together. In JUnit 5, they are separate annotations which you can use for different types of tests.
How to Fix:
-
Separate
@ParameterizedTest
: Remove the@Test
annotation and keep only the@ParameterizedTest
annotation for your parameterized test method.
public class JUnit5Example { private Calculator calculator = new Calculator(); //@Test <<----- Remove the @Test annotation @ParameterizedTest @CsvSource({ "1, 2, 3", "5, 5, 10", "10, 0, 10" public void testAdd(int a, int b, int expected) { int result = calculator.add(a, b); String message = "Summing " + a + " to " + b + " should be equal to " + expected; assertEquals(expected, calculator.add(a, b), message); // Other parts of your class...
-
Then,
Ensure Correct Imports:
Ensure that you have the proper imports for JUnit 5 annotations and assertions (
@ParameterizedTest
,@CsvSource
,assertEquals
). Verify that you’re importing these from theorg.junit.jupiter
package.
By separating the annotations correctly and providing proper parameterized test data through
@CsvSource
, your test should now execute without encountering the “No ParameterResolver registered” error.
Remember to organize your test runner to execute the tests properly. If you’re using JUnit Platform Console Launcher, make sure to include the necessary configurations to scan and run the tests.
If you want to learn more about JUnit ParameterizedTest please check this article: JUnit 5 Made Easy