如何在 JUnit 5 中使用其他 class 中定义的@MethodSource

How to use @MethodSource defined in other class in JUnit 5

有什么方法可以使用@MethodSource来使用其他class中定义的方法吗?

例如:下面的代码有效,因为 stringProvider 方法在相同的 class.

中定义
@ParameterizedTest
@MethodSource("stringProvider")
void methodSourceFromOtherClass(String word) {
    System.out.println(word);
    assertNotNull(word);
}

public static Stream<Arguments> stringProvider() {
    return Stream.of(
            Arguments.of("Values"),
            Arguments.of("From"),
            Arguments.of("MethodSource"));      
}

我有一些实用程序 classes,可以提供测试数据。如何在 @methodSource 中使用来自外部 classes 的方法?

从外部引用方法的语法 类

@MethodSource("fullyQualifiedClassName#methodName")

例如

@ParameterizedTest
@MethodSource("com.niraj.DataProvider#stringProvider")
void methodSourceFromOtherClass(String word) {
    System.out.println(word);
    assertNotNull(word);
}