Maksim Danilau
Maksim Danilau

Reputation: 100

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.

Upvotes: 4

Views: 5047

Answers (1)

Nicolai Parlog
Nicolai Parlog

Reputation: 51130

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

Upvotes: 8

Related Questions