Reputation: 100
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
Reputation: 51130
Make ArgClassProvider
static or a top-level class.
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:
static
, so it no longer references an instances of the outer class and the implicit constructor takes no parameterUpvotes: 8