junit5 @methodsource将参数传递到该方法
我正在寻找如何将参数传递到使用@methodsource Annotatio的Metod中,
例如,我需要调用@methodsource注释并将值传递到“ myfactorymethod”方法中。有可能吗? 这样的某个:
@ParameterizedTest
@MethodSource("MyFactoryMethod(10)")
void testWithMethodSource(int argument) {
assertNotEquals(9, argument);
}
static IntStream MyFactoryMethod(String var) {
return IntStream.range(0, var);
}
提前谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,至少在木星Junit v.5.8.2中,它不受支持。
允许您提供测试类中的方法名称以及外部类。
请注意,外部类中的方法必须通过完全合格的方法名称 - com.package.class# methotname 。
示例:
指测试类中的方法
@methodsource(“ getParams”)
外部方法
@methodsource(“ com.app.test.argumentsprovider#getargs”)
可以选择实现自己的自定义注释,如果不适合您,您可能会考虑这一点。
如何实现这种注释
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
有一个简单的解决方法,只需使用替代方法:
然后,您可以根据单个测试的要求创建
myFactoryMethod
的版本。额外的一步,但可行 - 是吗?上面的解决方案可能是上述简单情况的过度杀伤,但请想象您的
FactoryMethod
很复杂并创建许多参数,然后上述方法会偿还。There is a simple workaround, just use method override:
You can then create versions of
myFactoryMethod
as required for individual tests. An extra step, but workable - huh?The above solution may appear as an overkill for the above simple case, but imagine your
factoryMethod
is complex and creating many parameters, then the above method pays off.