JUnit5 multiple sources for different arguments (cartesian product)

Viewed 413

I am trying to write a test with JUnit 5 which should test multiple combinations of some parameters. Essentially I want to test some cartesian product of inputs from different sources. Consider the following test:

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
class CartesianProductTest {

    @ParameterizedTest
    @CsvFileSource(resources = { "values.csv" })
    void testIt(int input, int expected, int otherParameter) {
        assertEquals(expected, methodUnderTest(input, otherParameter));
    }
}

The problem is now that I only have input and expected in the values.csv and the otherParameter should be tested for some fixed values that methodUnderTest() always returns the expected value for all these values. Somehow I have to provide a cartesian product of all the values in my CSV and all the values otherParameter can take. I looked at https://stackoverflow.com/a/57648088/7962200 but this needs to either hardcode all my test cases or read the CSV manually to provide a Stream of the values. I thought more of something like

import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvFileSource;
class CartesianProductTest {

    @ParameterizedTest
    @MethodSource
    void testIt(int input, int expected, int otherParameter) {
        assertEquals(expected, methodUnderTest(input, otherParameter));
    }

    static Stream<Arguments> testIt() {
         return csvValues().flatMap(csv ->
             otherParams().map(otherParam -> Arguments.of(csv, otherParam)));
    }

    @CsvFileSource(resources = { "values.csv" })
    static Stream<Arguments> csvValues() {/* get somehow from Annotation */}
    @CsvFileSource(resources = { "otherparam.csv" })
    static Stream<Arguments> otherParams() {/* get somehow from Annotation */}
}
0 Answers
Related