javajunitjunit5parameterized-tests

How to pass multiple parallel streams to Junit5 Parameterized test?


I have two ArrayList<> Objects of equal lengths, and my Junit5 parameterized test has the syntax:

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


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

How can I return the elements from each ArrayList so that one list provides s1 and the other provide s2 as per the functionName function?


Solution

  • Naiv solution that prints

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

    with the following implementation

    @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();
    }