오랜만에 따뜻한 주말이다. 어느덧 2월도 중순이 된 3주 차를 되돌아본다.
파일을 읽어와서 이를 테스트 인자로 넘기는 테스트코드를 작성했다. 여러 방법을 시도해 봤는데 아래 코드가 가장 보기 좋은 구조였다. 다음에는 테스트 케이스도 인자로 받아서 DisplayName에 주입해주는 방식을 시도해보려고 한다.
class SolutionTest {
@ParameterizedTest(name = "{index}")
@MethodSource("testSource")
void test(int[] input, String expected) {
// given
Solution s = new Solution();
// when
// then
assertEquals(expected, s.solution(input));
}
private static Stream<Arguments> testSource() {
return loadData().stream().map(testCase -> Arguments.of(testCase.input, testCase.output));
}
private static List<TestCase> loadData() {
List<TestCase> result = new ArrayList<>();
for (int i = 1; i <= 3; i++) {
String inputFilePath = "src/main/BJ15651/source/input" + i + ".txt";
String outputFilePath = "src/main/BJ15651/source/output" + i + ".txt";
result.add(new TestCase(readInput(inputFilePath), readOutput(outputFilePath)));
}
return result;
}
private static int[] readInput(String filePath) {
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
StringTokenizer st = new StringTokenizer(br.readLine());
return new int[]{Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())};
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static String readOutput(String filePath) {
StringBuilder result = new StringBuilder();
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
String line = "";
while ((line = br.readLine()) != null) {
result.append(line + " ").append("\n");
}
return result.toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static class TestCase {
final int[] input;
final String output;
public TestCase(int[] input, String output) {
this.input = input;
this.output = output;
}
}
}