如何设置Junit5 @Methodsources?

问题描述 投票:0回答:1

我正在尝试使用5.11版本中引入的

@MethodSources
Javadoc

class MapFieldsTest {
    static Stream<Arguments> testGetIntCommon() {
        return Stream.of(
            Arguments.of("a", 10),
            Arguments.of("b", 20)
        );
    }

    static Stream<Arguments> testGetIntBroken() {
        return Stream.of(
            Arguments.of("c", null),
            Arguments.of("d", null)
        );
    }

    @ParameterizedTest
    @MethodSources({
        @MethodSource("testGetIntCommon"),
        @MethodSource("testGetIntBroken")
    })
    void testGetInt(String givenKey, Integer expected) {
        // THIS DOESN'T WORK
    }

    @ParameterizedTest
    @MethodSource("testGetIntCommon")
    void testGetIntCommon(String givenKey, Integer expected) {
        // THIS WORKS
    }
    
    @ParameterizedTest
    @MethodSource("testGetIntBroken")
    void testGetIntBroken(String givenKey, Integer expected) {
        // THIS WORKS
    }
}

但是我收到错误:

MapFieldsTest.testGetInt(String, Integer) » PreconditionViolation Configuration error: You must configure at least one set of arguments for this @ParameterizedTest

@MethodSources
设置可能存在什么问题?

java unit-testing junit5
1个回答
0
投票

从 JUnit 5.11 开始,

@MethodSource
是可重复的注释,
@MethodSources
用作支持容器。看来 JUnit 的注释处理不检查
@MethodSources
,而仅检查其嵌套元素。试试这个:

@ParameterizedTest
@MethodSource("testGetIntCommon"),
@MethodSource("testGetIntBroken")
void testGetInt(String givenKey, Integer expected) {
    ...
}
© www.soinside.com 2019 - 2024. All rights reserved.