Q:

How can I test multiple inputs from console?

I need to test input from console in my program:
Main.java - class with main logic.

public class Main {
    public static void main(final String[] args) {
        final Integer dividend = new IntegerReader().fetchIntegerNumber("Input dividend: ");
        final Integer divisor = new IntegerReader().fetchIntegerNumber("Input divisor: ");
        System.out.println(dividend);
        System.out.println(divisor);
    }
}

IntegerReader.java - reads integer number from input.

import java.util.Scanner;

public class IntegerReader {

    public Integer fetchIntegerNumber(final String message) {
        System.out.print(message);
        final Scanner scanner = new Scanner(System.in);
        final String inputString = scanner.nextLine();
        scanner.close();
        return Integer.valueOf(inputString);
    }
}

MainTest.java - test of Main.java class.

class MainTest {
    private final InputStream testInputStream = new ByteArrayInputStream("2\n3".getBytes());
    private final ByteArrayOutputStream testOutputStream = new ByteArrayOutputStream();
    private InputStream initialInputStream = System.in;
    private PrintStream initialOutputStream = System.out;

    @BeforeEach
    void setUpStreams() {
        System.setIn(testInputStream);
        System.setOut(new PrintStream(testOutputStream));
    }

    @AfterEach
    void restoreDefaultStreams() {
        System.setIn(initialInputStream);
        System.setOut(initialOutputStream);
        System.out.println("Test initial output stream");
    }

    @Test
    void fetchesTextFromInput() {
        Main.main(new String[]{});
        final String actual = testOutputStream.toString();
        assertThat(actual, is("Input dividend: Input divisor: 2\n3"));
    }
}

When I run test, I get exception: java.util.NoSuchElementException: No line found.
This exception is thrown, if I try to read second number (divisor) on line:
final String inputString = scanner.nextLine();

How can I fix test, so that exception will not occur?

A:

I would design the IntegerReader in the way that it accepts a scanner:
public class IntegerReader {

    private final Scanner scanner;

    public IntegerReader(Scanner theScanner) {
        this.scanner = theScanner;
    }

    public Integer fetchIntegerNumber(final String message) {
        System.out.print(message);
        final String inputString = scanner.nextLine();
        return Integer.valueOf(inputString);
    }
}

The main class would look like this:
public class Main {
    private Scanner scanner;

    public Main(Scanner theScanner) {
        this.scanner = theScanner;
    }
    public static void main(final String[] args) {
        new Main(new Scanner(System.in)).process();
    }

    public void process() {
        final Integer dividend = new IntegerReader(scanner).fetchIntegerNumber("Input dividend: ");
        final Integer divisor = new IntegerReader(scanner).fetchIntegerNumber("Input divisor: ");
        System.out.println(dividend);
        System.out.println(divisor);
    }
}

The test then looks the following way:
// this test still fails, but. ..
public class MainTest {
private final InputStream testInputStream = new ByteArrayInputStream("2\n3\n".getBytes());
    private final ByteArrayOutputStream testOutputStream = new ByteArrayOutputStream();
    private PrintStream initialOutputStream = System.out;

    @Before
    public void setUpStreams() {
        System.setOut(new PrintStream(testOutputStream));
    }

    @After
    public void restoreDefaultStreams() {
        System.setOut(initialOutputStream);
        System.out.println("Test initial output stream");
    }

    @Test
    public void fetchesTextFromInput() {
        Scanner scanner = new Scanner(testInputStream);
        Main main = new Main(scanner);
        main.process();
        final String actual = testOutputStream.toString();
        Assert.assertEquals("Input dividend: Input divisor: 2\n3", actual);
    }
}

In this way the scanner is not closed.
Note that the test is not green. I used JUnit, I don't know which technology you used.
Please note:

If I design a class I would not make it dependend on System.*, but give it some stream to work with. This eases testing.
if you use new anywhere in your code you need to know whether the class has the concern to do this or if someone else, a builder or factory should rather create this instance.

