Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Junit5 Error. You must provide at least one argument for this @ParameterizedTest

I'm trying to develop a parameterized test in JUnit 5, as in the example below.

@ParameterizedTest
@ArgumentsSource(ArgClassProvider.class)
void testAction_shouldSmth(ArgClass argClass) {
   //...
}


class ArgClassProvider implements ArgumentsProvider {

    @Override
    public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) throws Exception {
        return Stream.of(new ArgClass(), new ArgClass()).map(Arguments::of);
    }
}

Attempt to run test causes exception:

java.lang.NoSuchMethodException: com.ots.platform_sl.service.config.service.EarnMilesServiceTestHotels$ArgClassProvider.<init>()
...
org.junit.platform.commons.util.PreconditionViolationException: Configuration error: You must provide at least one argument for this @ParameterizedTest
...

You must provide at least one argument for this @ParameterizedTest

This message makes me feel, that I'm doing something wrong, am not I?

p. s. I have an assumption, that only args of primitive types are available.

like image 829
Maksim Danilau Avatar asked Oct 31 '25 13:10

Maksim Danilau


1 Answers

TL;DR

Make ArgClassProvider static or a top-level class.

Long Version

Take a closer look at the error message:

java.lang.NoSuchMethodException:
com.ots.platform_sl.service.config.service.EarnMilesServiceTestHotels$ArgClassProvider.<init>()

What you can see here is that Jupiter can't find a parameterless constructor for ArgClassProvider. The reason is that it's a non-static inner class, which means it's implicit constructor takes an instance of the outer class (in this case EarnMilesServiceTestHotels) as an argument.

To give your ArgumentsProvider implementation the parameterless constructor it requires, you have two options:

  • make it a proper class
  • make it static, so it no longer references an instances of the outer class and the implicit constructor takes no parameter
like image 149
Nicolai Parlog Avatar answered Nov 02 '25 04:11

Nicolai Parlog



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!