如何在 Junit 5 中动态传递多个输入和输出测试文件名?
How to Pass multiple Input and Output test file names dynamically In Junit 5?
我需要将多个文件一个一个地传递给下面的 BeforeAll 方法。我可以通过重复相同代码的不同测试 classes 来实现它。我想使用单个 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);
}
有人请教如何实现这一目标吗?
参考
How to pass a Input and Expected file name dynamically for BeforeAll Method | Junit 5
您可以创建 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());
});
我需要将多个文件一个一个地传递给下面的 BeforeAll 方法。我可以通过重复相同代码的不同测试 classes 来实现它。我想使用单个 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);
}
有人请教如何实现这一目标吗?
参考 How to pass a Input and Expected file name dynamically for BeforeAll Method | Junit 5
您可以创建 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());
});