junit5 @methodsource将参数传递到该方法

发布于 2025-01-31 06:06:40 字数 381 浏览 5 评论 0原文

我正在寻找如何将参数传递到使用@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风和你 2025-02-07 06:06:40

不幸的是,至少在木星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

帅气尐潴 2025-02-07 06:06:40

有一个简单的解决方法,只需使用替代方法:

@ParameterizedTest
@MethodSource("myFactoryMethod10")
void testWithMethodSource(int argument) {
    assertNotEquals(10, argument);
}

@ParameterizedTest
@MethodSource("myFactoryMethod9")
void testWithMethodSource(int argument) {
    assertNotEquals(9, argument);
}

public static IntStream myFactoryMethod10() {
   return factoryMethod(10);
}

public static IntStream myFactoryMethod9() {
   return factoryMethod(9);
}

static IntStream factoryMethod(String var) {
    return IntStream.range(0, var);
}

然后,您可以根据单个测试的要求创建myFactoryMethod的版本。额外的一步,但可行 - 是吗?

上面的解决方案可能是上述简单情况的过度杀伤,但请想象您的FactoryMethod很复杂并创建许多参数,然后上述方法会偿还。

There is a simple workaround, just use method override:

@ParameterizedTest
@MethodSource("myFactoryMethod10")
void testWithMethodSource(int argument) {
    assertNotEquals(10, argument);
}

@ParameterizedTest
@MethodSource("myFactoryMethod9")
void testWithMethodSource(int argument) {
    assertNotEquals(9, argument);
}

public static IntStream myFactoryMethod10() {
   return factoryMethod(10);
}

public static IntStream myFactoryMethod9() {
   return factoryMethod(9);
}

static IntStream factoryMethod(String var) {
    return IntStream.range(0, var);
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文