title: Testing console based applications/programs - Java
body: <p>All,</p>
  
  <p>I have written a PhoneBook application in Java that is command line based. The application basically asks for some details of user like Name, Age, Address and phone numbers and stores them in a file. Other operations involve looking up PhoneBook by name, phone number etc. All the details are entered through console.</p>
  
  <p>I am trying to write JUnit test cases for each of the functionalities that I have implemented but not able to figure out how to redirect <code>System.in</code> in the implementation code to something in my JUnit test methods that would supply those values when my actual code stops for user input?</p>
  
  <p>Example:</p>
  
  <p>My implementation code has:</p>
  
  <pre><code>BufferedReader is = new BufferedReader (new InputStreamReader(System.in));
  System.out.println("Please enter your name:");
  String name = is.readLine();             // My test cases stop at this line. How can I pass command line values i.e. redirect System.in to my test based values?
  </code></pre>
  
  <p>Hope it makes sense</p>