Nhu Nguyen 6 лет назад
Родитель
Сommit
c202614345

+ 5
- 0
src/main/java/com/zipcoder/console/Console.java Просмотреть файл

@@ -22,4 +22,9 @@ public class Console {
22 22
     public String nextLine() {
23 23
         return scanner.nextLine();
24 24
     }
25
+
26
+    public String ask() {
27
+        out.print("Hit or stay?");
28
+        return scanner.nextLine();
29
+    }
25 30
 }

+ 25
- 2
src/test/java/com/zipcoder/console/ConsoleTest.java Просмотреть файл

@@ -10,13 +10,20 @@ import java.io.InputStream;
10 10
 import java.io.PrintStream;
11 11
 
12 12
 public class ConsoleTest {
13
+    private PrintStream printStream;
14
+    private ByteArrayOutputStream outputStream;
15
+
16
+    @Before
17
+    public void setup() {
18
+        outputStream = new ByteArrayOutputStream();
19
+        printStream = new PrintStream(outputStream);
20
+
21
+    }
13 22
 
14 23
     @Test
15 24
     public void testPrint(){
16 25
         //Given
17 26
         String expectedString = "Hello";
18
-        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
19
-        PrintStream printStream = new PrintStream(outputStream);
20 27
         Console console = new Console(printStream, null);
21 28
 
22 29
         //When
@@ -39,4 +46,20 @@ public class ConsoleTest {
39 46
         //Then
40 47
         Assert.assertEquals(expectedString, actualString);
41 48
     }
49
+
50
+    @Test
51
+    public void testPrintAndRead(){
52
+        String question = "Hit or stay?";
53
+
54
+        String answer = "stay";
55
+        InputStream inputStream = new ByteArrayInputStream(answer.getBytes());
56
+        Console console = new Console(printStream, inputStream);
57
+
58
+        //When
59
+        String userInput = console.ask();
60
+
61
+        //Then
62
+        Assert.assertEquals(question, outputStream.toString());
63
+        Assert.assertEquals(answer, userInput);
64
+    }
42 65
 }