I don’t like how for parameterized tests with @MethodSource
, I have to put the methods in the companion object and have to annotate them with @JvmStatic
, too - that’s a lot of clutter. I also would like to have the source data methods close to the tests, which isn’t possible.
The best solution I came up with is to use @ArgumentsSource
together with a little helper class:
open class ArgProvider(vararg val data: Arguments) : ArgumentsProvider {
override fun provideArguments(
context: ExtensionContext?
): Stream<out Arguments> = Stream.of(*data)
}
With that in place, I can write tests such as
@ParameterizedTest
@ArgumentsSource(MyArgProvider::class)
fun test(a: Int, b: String) {
assertThat(a.toString()).isEqualTo(b)
}
class MyArgProvider : ArgProvider(
arguments(1, "1"),
arguments(-1, "-1")
)
I think this is already pretty close to what I want, but maybe there is a better way, e.g. without the need for a helper class. Any ideas?