title: Prevent JUnit test from getting stuck in while-loop
body: <p>I'm currently writing some JUnit tests for some assignments on Replit.com's Teams for education. I have a test that's getting stuck, I believe, because of a while loop in the main method. If the first input is valid, according to the program, the test runs. If the first input is invalid, the test gets stuck.</p>
  <p>Here's the test:</p>
  <pre><code>@Test
  public void validPW(){
  String correctOutput = &quot;Enter a password &gt;&gt;  Not enough uppercase letters!\nNot enough digits!\nReEnter a password &gt;&gt; Valid password\nGoodbye!&quot;;
    try{
      // IMPORTANT: Save the old System.out!
      PrintStream old = System.out;
  
      // Create a stream to hold the output
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      //PrintStream ps = new PrintStream(baos);
      System.setOut(new PrintStream(baos, false, &quot;UTF-8&quot;));
  
      // IMPORTANT: save old Sytem.in!
      InputStream is = System.in;
  
      // Set new System.in
      System.setIn(new ByteArrayInputStream(&quot;Iljvm4\nVaGN76js\n&quot;.getBytes()));
  
      // Calling main method should save main method's output as string literal to baos.
      String[] requiredArray = {&quot;Hello&quot;, &quot;There&quot;};
      ValidatePassword.main(requiredArray);
  
      // Put things back
      System.out.flush();
      System.setOut(old);
  
      //Restore
      System.setIn(is);
  
  
      assertEquals(correctOutput, baos.toString());
    }catch(IOException ioe){
       new IOException(&quot;i/o problem - test not executed\n&quot;);
     }
  }
  </code></pre>
  <p>Here's the program:</p>
  <pre><code>import java.util.*;
  
  public class ValidatePassword {
      public static void main(String[] args) {
  
          String passWord;
          boolean Valid = false;
  
          final int NUM = 2; // two digits and two Upper case letters
          // counters to count the required digits and letters
          int upperCount = 0;
          int lowerCount = 0;
          int digitCount = 0;
  
          while (!Valid) {
              
              Scanner in = new Scanner(System.in);
              int numSpaces = 0;
              System.out.print(&quot;Enter a password &gt;&gt; &quot;);
              passWord = in.next();
              in.nextLine(); // capture dangling newline char.
              // Using a for loop to iterate over each character in the String
              for (int i = 0; i &lt; passWord.length(); i++) {
                  char ch = passWord.charAt(i);
                  if (Character.isUpperCase(ch)){ // Using the Character class's methods
                      upperCount++;
                  }
                  else if (Character.isLowerCase(ch)){
                      lowerCount++;
                  }
                  else if (Character.isDigit(ch)){
                      digitCount++;
                  }
              }
  
              if (upperCount &gt;= NUM &amp;&amp; lowerCount &gt;= 3 &amp;&amp; digitCount &gt;= NUM) {
                  System.out.println(&quot;Valid password\nGoodbye!&quot;);
                  Valid = true;   
              } else {                
                  if (upperCount &lt; NUM)
                      System.out.println(&quot;Not enough uppercase letters!&quot;);
                  if (lowerCount &lt; 3)
                      System.out.println(&quot;Not enough lowercase letters!&quot;);
                  if (digitCount &lt; NUM)
                      System.out.println(&quot;Not enough digits!&quot;);
                  System.out.print(&quot;Re&quot;);
                  // Resetting the counters if not a valid password
                  upperCount = 0;
                  lowerCount = 0;
                  digitCount = 0;
  
              }            
          }
      }
  }
  </code></pre>