Saturday, 15 June 2019

org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered for parameter [java.lang.String arg0] in method [void ParametrizedTest.test(java.lang.String)].

I was experimenting with JUnit 5 and Parameterized Tests and hit the following error.

org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered for parameter [java.lang.String arg0] in method [void ParametrizedTest.test(java.lang.String)].

This was the code that was used to trigger the error.


import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class ParametrizedTest {

    @Test
    @ParameterizedTest
    @MethodSource("stringProvider")
    void test(String string) {
        assertTrue(true);
    }

    public static Stream stringProvider() {
        return Stream.of("", null, "123");
    }
}

The issue is that the test method is annotated with both @Test and @ParameterizedTest. When using parameterized tests, you should only annotate your test method with @ParameterizedTest and not @Test. Removing the @Test annotation will prevent this error.


import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class ParametrizedTest {

    @ParameterizedTest
    @MethodSource("stringProvider")
    void test(String string) {
        assertTrue(true);
    }

    public static Stream stringProvider() {
        return Stream.of("", null, "123");
    }
}

1 comment: