title: How to mock System.in?
body: <p>Could you explain me please how to mock <code>System.in</code> correctly?</p>
  
  <p>I have a simple test and I assign values to </p>
  
  <pre><code>@Test
  public void testBarCorrect() {
      System.setIn(new ByteArrayInputStream("7\n2\n3".getBytes()));
      someService().consume();
  }
  </code></pre>
  
  <p>Under the hood I do nothing but <code>new Scanner(System.in);</code></p>
  
  <p>The caveat is that sometimes my test is green but in 80% it throws an <code>Exception "java.util.NoSuchElementException: No line found".</code></p>
  
  <p>So, if I press run on a test it misbehaves.</p>
  
  <p>Could you give me any hints?</p>
  
  <p>My test:</p>
  
  <pre><code>public class RecordServiceCTLIntegrationTest {
  
      private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
      private final PrintStream originalOut = System.out;
      private static final InputStream DEFAULT_STDIN = System.in;
  
      @Before
      public void setUpStreams() {
          System.setOut(new PrintStream(outContent));
          System.setIn(DEFAULT_STDIN);
      }
  
      @After
      public void rollbackChangesToStdin() {
          System.setOut(originalOut);
          System.setIn(DEFAULT_STDIN);
      }
  
      @Test
      public void testBarCorrect() {
          System.setIn(new ByteArrayInputStream("5\n1\nB\n21\n\n".getBytes()));
          buildRecordServiceCTL().run();
          assertEquals("Enter Room Id: No active booking for room id: 5\n" +
                  "Enter Room Id: B:\tBar Fridge\n" +
                  "R:\tRestaurant\n" +
                  "S:\tRoom Service\n" +
                  "Enter service typeEnter cost: Service Bar Fridge has been added for the roomNumber 1\n" +
                  "Hit &lt;enter&gt; to continuePay for service completed\n", outContent.toString());
      }
  
      @Test
      public void testRestaurantCorrect() {
          System.setIn(new ByteArrayInputStream("5\n1\nR\n21\n\n".getBytes()));
          buildRecordServiceCTL().run();
          assertEquals("Enter Room Id: No active booking for room id: 5\n" +
                  "Enter Room Id: B:\tBar Fridge\n" +
                  "R:\tRestaurant\n" +
                  "S:\tRoom Service\n" +
                  "Enter service typeEnter cost: Service Restaurant has been added for the roomNumber 1\n" +
                  "Hit &lt;enter&gt; to continuePay for service completed\n", outContent.toString());
  
      }
  
      @Test
      public void testRoomServiceCorrect() {
  
          System.setIn(new ByteArrayInputStream("5\n1\nS\n21\n\n".getBytes()));
          buildRecordServiceCTL().run();
          assertEquals("Enter Room Id: No active booking for room id: 5\n" +
                  "Enter Room Id: B:\tBar Fridge\n" +
                  "R:\tRestaurant\n" +
                  "S:\tRoom Service\n" +
                  "Enter service typeEnter cost: Service Room Service has been added for the roomNumber 1\n" +
                  "Hit &lt;enter&gt; to continuePay for service completed\n", outContent.toString());
      }
  
      @Test(expected = NoSuchElementException.class)
      public void testRoomException() {
          System.setIn(new ByteArrayInputStream("-1\n\n".getBytes()));
          buildRecordServiceCTL().run();
      }
  
      @Test
      public void cancel() {
          System.setIn(new ByteArrayInputStream("5\n1\nS\n20\n".getBytes()));
          assertEquals("", outContent.toString());
      }
  
      private RecordServiceCTL buildRecordServiceCTL() {
          Hotel hotel = new Hotel();
          final Guest guest = new Guest("name", "address", 123);
          final Room room = new Room(1, SINGLE);
          final CreditCard card = new CreditCard(VISA, 123, 123);
          final Booking booking = new Booking(guest, room, new Date(), 10, 1, card);
          hotel.activeBookingsByRoomId.put(room.getId(), booking);
          final Map&lt;Integer,Room&gt; map = new HashMap&lt;&gt;();
          map.put(room.getId(), room);
          hotel.roomsByType.put(SINGLE, map);
          return new RecordServiceCTL(hotel);
      }
  
  }
  </code></pre>
  
  <p>And exception occurs in run method in:</p>
  
  <pre><code>if (stdin == null) {
      stdin = new Scanner(System.in);
  }
  String ans = stdin.nextLine();
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/TsGwA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TsGwA.png" alt="enter image description here"></a></p>