Course – LS – All
announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

When it comes to testing the code that relies on user input from the console, the process can become quite challenging. Moreover, testing user input scenarios is a crucial part of console-based or standalone applications since we need to ensure the proper handling of different inputs.

Throughout this tutorial, we’ll take a look at the ways we can test the System.in using JUnit.

2. Understanding the System Class

Before we dive in, let’s look at the System class. It’s a final class from the java.lang package.

The class provides access to the standard input and output streams through the in and out variables. Similar to the out variable, the System class has the err variable that represents the standard error output stream.

Furthermore, those variables allow us to read from and write into the console. Using these streams, we allow users to interact with our application from the console.

Next, System.in returns an InputStream which is already open to read data from the standard input. Using System.in, we can redirect the input stream going from the keyboard to the CPU into our application.

3. Example Input

Let’s start with the simple example we’ll use throughout this tutorial:

public static final String NAME = "Name: ";

public static String readName() {
    Scanner scanner = new Scanner(System.in);
    String input = scanner.next();
    return NAME.concat(input);
}

Java offers the Scanner class, which allows us to read input from various sources, including the standard keyboard input. Moreover, it provides the easiest way to read user input. Using Scanner, we can read any primitive data type or String.

In our example method, we use the next() method to read input from the user. In addition, the method reads the next word from the input as a String.

4. Using Core Java

The first approach to unit testing the standard input includes the functionalities provided by the Java API.

We can utilize System.in to create a custom InputStream and simulate user input during testing.

However, before writing the unit test, let’s write the provideInput() helper method in our test class:

void provideInput(String data) {
    ByteArrayInputStream testIn = new ByteArrayInputStream(data.getBytes());
    System.setIn(testIn);
}

Inside the method, we create a new ByteArrayInputStream and pass our desired input as a byte array.

Additionally, we use the System.setIn() method to set a custom input stream as the input for System.in.

Now, let’s write a unit test for our example method. We can call the readName() method of our application class, which now reads our custom input:

@Test
void givenName_whenReadFromInput_thenReturnCorrectResult() {
    provideInput("Baeldung");
    String input = Application.readName();
    assertEquals(NAME.concat("Baeldung"), input);
}

5. Using the System Rules Library and JUnit 4

Now, let’s see how to test standard input using the System Rules library and JUnit 4.

Firstly, let’s add a required dependency to our pom.xml:

<dependency>
    <groupId>com.github.stefanbirkner</groupId>
    <artifactId>system-rules</artifactId>
    <version>1.19.0</version>
    <scope>test</scope>
</dependency>

The library provides JUnit rules for testing the code that relies on System.in and System.out.

Additionally, it allows us to redirect input and output streams during the test execution, which makes it easy to simulate user input.

Secondly, to test System.in, we’ll need to define a new TextFromStandardInputStream rule. We’ll use the emptyStandardInputStream() method to initialize the variable with an empty input stream:

@Rule
public final TextFromStandardInputStream systemIn = emptyStandardInputStream();

Finally, let’s write the unit test:

@Test
public void givenName_whenReadWithSystemRules_thenReturnCorrectResult() {
    systemIn.provideLines("Baeldung");
    assertEquals(NAME.concat("Baeldung"), Application.readName());
}

Additionally, we use the provideLines() method that accepts varargs and sets them as the inputs.

Furthermore, the original System.in is restored after the test execution.

6. Using the System Lambda Library and JUnit 5

It’s important to mention System Rules doesn’t support JUnit 5 by default. However, they provide a System Lambda library we can use with JUnit 5.

We’ll need an additional dependency in our pom.xml:

<dependency>
    <groupId>com.github.stefanbirkner</groupId>
    <artifactId>system-lambda</artifactId>
    <version>1.2.1</version>
    <scope>test</scope>
</dependency>

Now, let’s use System Lambda in our test:

@Test
void givenName_whenReadWithSystemLambda_thenReturnCorrectResult() throws Exception {
    withTextFromSystemIn("Baeldung")
      .execute(() -> assertEquals(NAME.concat("Baeldung"), Application.readName()));
}

Here, we use the withTextFromSystemIn() static method provided in SystemLambda class to set the input lines that will be available from System.in.

7. Using the System Stubs Library and JUnit 4

Additionally, we can test the standard input using JUnit 4 and the System Stubs library.

Let’s add the required dependency:

<dependency>
    <groupId>uk.org.webcompere</groupId>
    <artifactId>system-stubs-junit4</artifactId>
    <version>2.0.2</version>
    <scope>test</scope>
</dependency>

Next, let’s create the SystemInRule and pass the desired input values:

@Rule
public SystemInRule systemInRule = new SystemInRule("Baeldung");

Now, we can use the created rule in our unit test:

@Test
public void givenName_whenReadWithSystemStubs_thenReturnCorrectResult() {
    assertThat(Application.readName()).isEqualTo(NAME.concat("Baeldung"));
}

8. Using the System Stubs Library and JUnit 5

To test System.in using System Stubs and JUnit 5, we’ll need to add another dependency:

<dependency>
    <groupId>uk.org.webcompere</groupId>
    <artifactId>system-stubs-jupiter</artifactId>
    <version>2.0.2</version>
</dependency>

To provide the input values, we’ll use the withTextFromSystemIn() method:

@Test
void givenName_whenReadWithSystemStubs_thenReturnCorrectResult() throws Exception {
    SystemStubs.withTextFromSystemIn("Baeldung")
      .execute(() -> {
        assertThat(Application.readName())
          .isEqualTo(NAME.concat("Baeldung"));
      });
}

9. Conclusion

In this article, we learned how to test System.in with both JUnit 4 and JUnit 5.

Through the first approach, we learned how to customize System.in using the core Java features. In the second approach, we saw how to use the System Rules library. Next, we learned how to use the System Lambda library to write tests with JUnit 5. Finally, we saw how to use the System Stubs library.

As always, the entire source code of this article is available over on GitHub.

Course – LS – All
announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

res – REST with Spring (eBook) (everywhere)
2 Comments
Oldest
Newest
Inline Feedbacks
View all comments