简体   繁体   中英

How to Pass multiple Input and Output test file names dynamically In Junit 5?

I need to pass multiple files one by one to the below BeforeAll method. I can able to achieve it with different test classes of duplication of same code. I would like to optimize it using single class.

@BeforeAll
static void setUp() throws IOException {
    inputList = readInput(CommonTestConstants.FilePath + "/Input1.json");
    expectedList = readExpected(CommonTestConstants.FilePath + "/Expected1.json");
    assertEquals("Checking size of both list", inputList.size(), expectedList.size());
}

static Stream<Arguments> Arguments() {
    return IntStream.range(0, inputList.size())
                .mapToObj(i -> Arguments.of(inputList.get(i), expectedList.get(i)));
}

@ParameterizedTest
@DisplayName("Parameterized Test For First Input")
@MethodSource("Arguments")
void testFact(Object object, ExpectedObject expected) throws Exception {
    Outcome outcome = processExpectedJson(object);
    assertEquals(expected, outcome);
}

Any one please advise to achieve this?

Reference How to pass a Input and Expected file name dynamically for BeforeAll Method | Junit 5

You can create a Map of file names and then iterate iver it as below,

Map<String, String> files = Map.of("/Input1.json", "/Expected1.json",
                                    "Input1.json", "/Expected2.json");

files.forEach((k,v)->{
    inputList = readInput(CommonTestConstants.FilePath + k);
    expectedList = readExpected(CommonTestConstants.FilePath + v);
    assertEquals("Checking size of both list",
            inputList.size(), expectedList.size());
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM