Skip to content

Commit 87aa862

Browse files
committed
test: 입출력 구현체 테스트 작성
1 parent b4f3cfa commit 87aa862

3 files changed

Lines changed: 89 additions & 0 deletions

File tree

calculator/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ repositories {
1212
dependencies {
1313
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
1414
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
15+
16+
// https://mvnrepository.com/artifact/org.assertj/assertj-core
17+
testImplementation 'org.assertj:assertj-core:3.24.2'
1518
}
1619

1720
test {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.wonu606.io;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.io.ByteArrayInputStream;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import org.junit.jupiter.api.DisplayName;
9+
import org.junit.jupiter.api.Test;
10+
11+
class ConsoleInputTest {
12+
13+
@Test
14+
@DisplayName("메뉴 입력")
15+
void testMenuSelectionInput() throws IOException {
16+
String menuSelection = "1";
17+
InputStream in = new ByteArrayInputStream(menuSelection.getBytes());
18+
System.setIn(in);
19+
20+
Input input;
21+
assertThat(menuSelection).isEqualTo(input.getInput());
22+
input.tearDown();
23+
}
24+
25+
@Test
26+
@DisplayName("표현식 입력")
27+
void testExpressionInput() throws IOException {
28+
String expression = "2 + 3";
29+
InputStream in = new ByteArrayInputStream(expression.getBytes());
30+
System.setIn(in);
31+
32+
Input input;
33+
assertThat(expression).isEqualTo(input.getInput());
34+
input.tearDown();
35+
}
36+
37+
@Test
38+
@DisplayName("개행 입력")
39+
void testNewLineInput() throws IOException {
40+
String newLine = "\n";
41+
InputStream in = new ByteArrayInputStream(newLine.getBytes());
42+
System.setIn(in);
43+
44+
Input input;
45+
assertThat("").isEqualTo(input.getInput());
46+
input.tearDown();
47+
}
48+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.wonu606.io;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.io.ByteArrayOutputStream;
6+
import java.io.OutputStream;
7+
import java.io.PrintStream;
8+
import org.junit.jupiter.api.DisplayName;
9+
import org.junit.jupiter.api.Test;
10+
11+
class ConsolePrinterTest {
12+
13+
@Test
14+
@DisplayName("정수 출력")
15+
void testIntegerPrint() {
16+
OutputStream out = new ByteArrayOutputStream();
17+
System.setOut(new PrintStream(out));
18+
19+
Print printer;
20+
int number = -1;
21+
printer.print(number);
22+
23+
assertThat(Integer.toString(number)).isEqualTo(out.toString().trim());
24+
}
25+
26+
@Test
27+
@DisplayName("문자열 출력")
28+
void testStringPrint() {
29+
OutputStream out = new ByteArrayOutputStream();
30+
System.setOut(new PrintStream(out));
31+
32+
Print printer;
33+
String str = "Hello World";
34+
printer.print(str);
35+
36+
assertThat(str).isEqualTo(out.toString().trim());
37+
}
38+
}

0 commit comments

Comments
 (0)