【问题标题】:How to Pass multiple Input and Output test file names dynamically In Junit 5?如何在 Junit 5 中动态传递多个输入和输出测试文件名?
【发布时间】:2020-10-28 11:50:39
【问题描述】:

我需要将多个文件一个一个地传递给下面的 BeforeAll 方法。我可以通过重复相同代码的不同测试类来实现它。我想使用单个类对其进行优化。

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

请任何人建议实现这一目标?

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

【问题讨论】:

    标签: java spring-boot unit-testing junit junit5


    【解决方案1】:

    您可以创建一个Map 的文件名,然后如下迭代它,

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-28
      • 2017-05-25
      • 2016-08-03
      相关资源
      最近更新 更多