【问题标题】:How to pass multiple parallel streams to Junit5 Parameterized test?如何将多个并行流传递给 Junit5 参数化测试?
【发布时间】:2018-08-16 05:48:39
【问题描述】:

我有两个长度相等的 ArrayList 对象,我的 Junit5 参数化测试的语法如下:

@ParamterizedTest
@MethodSource("dummyfunction");
void functionName(String s1, String s2)
{
.....
.....
}


private Stream<Arguments> dummyfunction()
{
     ArrayList<String> arr1;
     ArrayList<String> arr2;
     .....
     .....
    return something;
}

如何从每个 ArrayList 中返回元素,以便一个列表提供 s1 而另一个列表提供 s2 根据 functionName 功能?

【问题讨论】:

  • arr1arr2 的长度相等,您想要所有对,即(arr1.get(0), arr2.get(0)), (arr1.get(1), arr2.get(1)), ...,还是想要两个列表中元素的所有排列?
  • 你提到的所有对,而不是排列

标签: java junit junit5 parameterized-tests


【解决方案1】:

打印的朴素解决方案

s1 = [1], s2 = [a]
s1 = [2], s2 = [b]
s1 = [3], s2 = [c]

使用以下实现

@ParameterizedTest
@MethodSource("dummyFunction")
void functionName(String s1, String s2) {
    System.out.println("s1 = [" + s1 + "], s2 = [" + s2 + "]");
}

static Stream<Arguments> dummyFunction() {
    List<String> list1 = List.of("1", "2", "3");
    List<String> list2 = List.of("a", "b", "c");

    Assertions.assertEquals(list1.size(), list2.size());

    List<Arguments> arguments = new ArrayList<>();
    for (int i = 0; i < list1.size(); i++) {
        arguments.add(Arguments.of(list1.get(i), list2.get(i)));
    }

    return arguments.stream();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-16
    • 1970-01-01
    • 1970-01-01
    • 2019-06-30
    • 1970-01-01
    • 2017-01-09
    • 2020-11-08
    • 2019-04-08
    相关资源
    最近更新 更多