Close

JUnit - How to test user command line Input in Java?

[Last Updated: Mar 21, 2021]

JUnit Java Command Line 

Following example shows how to unit test command line input in Java.

Example

A command line app to test

package com.logicbig.example;

import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;

public class UserInputExample {
  private Scanner scanner;
  private PrintStream printStream;

  public UserInputExample(InputStream inputStream, PrintStream printStream) {
      this.scanner = new Scanner(inputStream);
      this.printStream = printStream;
  }

  public void start() {
      printStream.println("Enter number 1");
      int x = scanner.nextInt();
      printStream.println("Enter number 2");
      int y = scanner.nextInt();
      printStream.println("output: " + (x + y));
  }

  public static void main(String[] args) {
      UserInputExample userInputExample = new UserInputExample(System.in, System.out);
      userInputExample.start();
  }
}

Unit testing by using System.setIn() and System.setOut()

package com.logicbig.example;

import org.junit.Assert;
import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintStream;

public class UserInputExampleTest {

  @Test
   public void mainTest(){
      InputStream stdin = System.in;
      System.setIn(new ByteArrayInputStream("3\n4\n".getBytes()));

      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
      PrintStream ps = new PrintStream(byteArrayOutputStream);
      PrintStream stdout = System.out;
      System.setOut(ps);

      UserInputExample.main(new String[0]);

      System.setIn(stdin);
      System.setOut(stdout);

      String outputText = byteArrayOutputStream.toString();
      String key = "output:";
      String output = outputText.substring(outputText.indexOf(key) + key.length()).trim();
      Assert.assertEquals(output, "7");
   }
}
D:\java-test-user-command-line-input>mvn -q test -Dtest=UserInputExampleTest.java

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.logicbig.example.UserInputExampleTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.058 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

Unit testing by providing InputStream and PrintStream

package com.logicbig.example;

import org.junit.Assert;
import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

public class UserInputExampleTest2 {

  @Test
  public void userInputTest() {
      ByteArrayInputStream inputStream = new ByteArrayInputStream("3\n4\n".getBytes());
      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
      PrintStream ps = new PrintStream(byteArrayOutputStream);
      UserInputExample userInputExample = new UserInputExample(inputStream, ps);
      userInputExample.start();
      String outputText = byteArrayOutputStream.toString();
      String key = "output:";
      String output = outputText.substring(outputText.indexOf(key) + key.length()).trim();
      Assert.assertEquals(output, "7");
  }
}
D:\java-test-user-command-line-input>mvn -q test -Dtest=UserInputExampleTest2.java

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.logicbig.example.UserInputExampleTest2
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.051 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

Example Project

Dependencies and Technologies Used:

  • junit 4.12: JUnit is a unit testing framework for Java, created by Erich Gamma and Kent Beck.
  • JDK 1.8
  • Maven 3.6.3

java-test-user-command-line-input Select All Download
  • java-test-user-command-line-input
    • src
      • main
        • java
          • com
            • logicbig
              • example
      • test
        • java
          • com
            • logicbig
              • example
                • UserInputExampleTest2.java

    See Also