I'm looking for how to pass a parameter into a metod that use the @MethodSource annotatio in Junit5
For example I need to invoke @MethodSource annotation and passing a value into the method "MyFactoryMethod". It is possible? Sometings like this:
@ParameterizedTest
@MethodSource("MyFactoryMethod(10)")
void testWithMethodSource(int argument) {
assertNotEquals(9, argument);
}
static IntStream MyFactoryMethod(String var) {
return IntStream.range(0, var);
}
Thanks in advance
Unfortunately, it's not supported, at least in Jupiter JUnit v.5.8.2.
You are allowed to provide names of methods within the test class as well as external classes. Note that methods in external classes must be referenced by fully qualified method name - com.package.Class#methodName.
Example:
Referring to a method within the test class
@MethodSource("getParams")
External methods
@MethodSource("com.app.test.ArgumentsProvider#getArgs")
There is an option to implement a custom annotation of your own, if nothing else suits you maybe consider that.
Example of how to implement such annotation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With