![](/img/trans.png)
[英]How to pass a Input and Expected file name dynamically for BeforeAll Method | Junit 5
[英]How to Pass multiple Input and Output test file names dynamically In Junit 5?
我需要將多個文件一個一個地傳遞給下面的 BeforeAll 方法。 我可以通過重復相同代碼的不同測試類來實現它。 我想使用單個 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);
}
任何人請建議實現這一目標?
您可以創建文件名的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());
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.