junit5 - 如何使用在JUnit 5的其他类中定义的@MethodSource

标签 junit5

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

例如下面的代码有效,因为stringProvider方法是在同一类中定义的。

 @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"));

    }

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

最佳答案

从外部类引用方法的语法

@MethodSource("fullyQualifiedClassName#methodName")

例如

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

关于junit5 - 如何使用在JUnit 5的其他类中定义的@MethodSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53975605/

相关文章:

测试中的 JUnit5 断言计数

kotlin - 如何防止 JUnit5 打印堆栈跟踪?

java - 忽略 Junit5 中的测试

java - 在 JUNIT 中运行属于多个类别的测试用例

maven - 如何使用 Maven Surefire 插件调用 JUnit5 测试运行器/Junit 标记表达式?

java - 如何断言已将适当的值分配给私有(private)字段?

java - 如果没有至少一个 TestEngine,则无法创建 Launcher;考虑在 Junit 5 的类路径中添加一个引擎实现 JAR

java - Junit 5 @SpringBootTest 可执行 jar

android - 如何使用 ParameterResolver 在 JUnit 5 中注入(inject)多个扩展值

java - 将参数从 JUnit 5 @BeforeEach 传递到单独的测试