title: Test InputStream with Junit
body: <p>I'm trying to test a function "DataFeatures" in my class "UserInput".</p>
  
  <p>It doesn't matter what arguments I give in the test, It always pass. </p>
  
  <p><strong>Fields and constructor</strong> </p>
  
  <pre><code>public @Getter class UserInput {
      FileType type;
      FileOperation operation;
      SynchronizationMethod method;
      String path;
      private static InputReader in = new InputReader();
  
      public UserInput() {
          // Dont need to do anything
      }
  </code></pre>
  
  <p><strong>Function to test</strong>  </p>
  
  <pre><code>    void getDataFeatures() {
      System.out.println("For encryption press 1");
      System.out.println("For decryption press 0");
      operation = FileOperation.fromInt(in.nextInt());
  
      System.out.println("For a file choose 1");
      System.out.println("For an entire directory choose 0");
      type = FileType.fromInt(in.nextInt());
  
      if (type == FileType.DIR) {
          System.out.println("For sync press 1");
          System.out.println("For async press 0");
          method = SynchronizationMethod.fromInt(in.nextInt());
      }
  }
  </code></pre>
  
  <p><strong>My test</strong></p>
  
  <pre><code>public class UserInputTest {
  
  UserInput UI;
  private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
  private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
  private final PrintStream oldStdOut = System.out;
  private final PrintStream oldStdErr = System.err;
  private final InputStream oldStdIn = System.in;
  
  @Before
  public void initlize(){
      System.setOut(new PrintStream(outContent));
      System.setErr(new PrintStream(errContent));
      UI = new UserInput();
  }
  
  @Test
  public void getDataFeaturesTest() {
      String data = "0" + "\n0" + "\n0";
      System.setIn(new ByteArrayInputStream(data.getBytes()));
      UI.getDataFeatures();
      System.out.println(UI.getOperation());
      assertThat(UI.getOperation(), is(equalTo(FileOperation.decryption)));
      assertThat(UI.getType(), is(equalTo(FileType.FILE)));
      assertThat(UI.getMethod(), is(equalTo(SynchronizationMethod.SYNC)));
  
  
  }
  
  @After
  public void cleanUpStreams() {
      System.setOut(oldStdOut);
      System.setErr(oldStdErr);
      System.setIn(oldStdIn);
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>Note 1</strong>: FileOperation, FileType and SynchronizationMethod are all enums that get 1 or 0. </p>
  
  <p>Example of SynchronizationMethod:</p>
  
  <pre><code>public enum SynchronizationMethod {
  SYNC(1), ASYNC(0);
  
  private int method;
  
  private SynchronizationMethod(int meth) {
      this.method = meth;
  }
  
  public static SynchronizationMethod fromInt(int meth) {
      for (SynchronizationMethod SM : SynchronizationMethod.values()) {
          if (SM.method == meth) {
              return SM;
          }
      }
      throw new IllegalArgumentException("No constant with method " + meth + " found");
  }
  
  public String toString(){
      if (method == 1){
          return "Sync";
      }
      else if(method == 0){
          return "ASync";
      }
      else{
          throw new IllegalArgumentException("No constant with method " + method + " found in toString");
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>Solution</strong> </p>
  
  <p>The problem was in the class InputReader in the constructor function.</p>
  
  <pre><code>    public InputReader() {
      reader = new BufferedReader(new InputStreamReader(System.in));
      tokenizer = null;
  }
  </code></pre>
  
  <p>The reader in this function and the Input Stream in the Junit function were disconnenct as suggested in the comments</p>