简体   繁体   中英

How to simulate user input for Scanner?

I have a class in which the user interacts on a terminal window and types in certain options, based on those options it makes a switch and uses certain methods; I need to use a scanner to detect user input.

I tried for a few days to make a test class to simulate user input but I cannot find a proper way to do it, as I cannot simulate System.in for the scanner and neither I have found any concrete information, I have seen something about buffering but I cannot use that.

Here is an attempt, which results in a nullPointerException for the scanner - because there is no input detected.. I also tried to sleep and then set the input.

An example of simulating System.in for Scanner would be much appreciated.

public void test1addItem()
{
    InputStream input = new ByteArrayInputStream("".getBytes());
    String data1="1"; //Add an item option
    String data2="bread"; //The item to add

    input = new ByteArrayInputStream(data1.getBytes());
    //System.out.println("DATA1="+input);
    System.out.println("TEMP - 1");
    System.setIn(input);
    System.out.println("TEMP - 2");
    tsl.start(); //reference to the class which I am testing
    System.out.println("TEMP - 3");
    try {
        Thread.sleep(2000);
    } catch(InterruptedException ex) {
        Thread.currentThread().interrupt();
    }
    System.out.println("TEMP - 4");
    input = new ByteArrayInputStream(data2.getBytes());
    System.out.println("TEMP - 5");
    System.setIn(input);
    System.out.println("TEMP - 6");
}

It stops at TEMP - 2, as it is a recursive method until a certain option is given to terminate the program.

您是否尝试过重新分配System.in?

System.setIn(new ByteArrayInputStream("data".getBytes()));

The fundamental reasoning behind this idea is flawed. I assume you want to do something like

scanner.read(2);

However, Scanner is designed to read from InputStreams such as Standard Input and Files. It needs a source to read from and a constant such as 2 is simply an invalid source.

A problem also arises if you were to use a method that was to feed 2 in as input when you call readLine(). Scanners only start reading when you call the read method and they don't stop until they have finished reading. So if you were to do

String s = scanner.readLine();
scanner.feed("hi");

the code would never reach the second line. If you were to do

scanner.feed("hi");
String s = scanner.readLine();

the scanner would never see "hi" because it was fed prior to the Scanner's reading.

You may be able to set up some sort of stream that just outputs constant "hi"s, but it's far more practical to just simulate the input yourself from System.in or a file.

You can also just set the variable to what would have been inputted! For example:

String s = "hi";   //scanner.readLine();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM