title: JUnit test user input with Mockito
body: <p>I have a problem with the Mocking Framework Mockito. I want to test the input the user types in the console. Without Mocking it works great but with it I don't know how to get the return value from the function which get's the input in the console.</p>
  
  <p><strong>Without Mockito:</strong></p>
  
  <p>Read input method</p>
  
  <pre><code>public int readUserInput() {
      BufferedReader console = new BufferedReader(new InputStreamReader(
              System.in));
      return Integer.parseInt(console.readLine());
      ...
  }
  </code></pre>
  
  <p>Test method:</p>
  
  <pre><code>public class TestClass {
      Controller contr = new Controller();
      @Test
      public void testUserInput() {
          System.setIn(new ByteArrayInputStream("1".getBytes()));
          assertEquals(1, contr.readUserInput());
          System.setIn(System.in);
      }
  }
  </code></pre>
  
  <p>But now how to write the test with Mockito. I tried several ways but none of them worked. I got all the time a 0 and not the number I wrote in the <code>ByteArrayInputStream</code>.</p>
  
  <p>Thanks in advance!</p>