0

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.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
4673_j
  • 477
  • 1
  • 6
  • 20
  • *"Here is an attempt, which results in a nullPointerException for the scanner - because there is no input detected"* Stacktrace? – Tom Oct 20 '15 at 19:58
  • *"It stops at TEMP - 2, as it is a recursive method until a certain option is given to terminate the program."* So why don't you provide that certain option as the input instead of using an empty String? – Tom Oct 20 '15 at 19:59
  • Why not using `new Scanner(new File("simulatedUserInput.txt"))` rather than trying to mock `System.in`? – Luiggi Mendoza Oct 20 '15 at 19:59
  • These days everyone is using the Scanner class and becomes confused, what about reading the InputStream from "System.in"? – Stefan Oct 20 '15 at 20:02
  • I cannot access the scanner, therefore I cannot change its input – 4673_j Oct 20 '15 at 20:05
  • Already read the oracle documentation, still I cannot find a way to simulate user input – 4673_j Oct 20 '15 at 20:08

2 Answers2

0

Have you tried re-assigning System.in?

System.setIn(new ByteArrayInputStream("data".getBytes()));
  • I did and this method got me one step further, but on the second attempt it gives the error as for certain methods it takes the user to another menu where it needs to type in some data, and there it fails.. – 4673_j Oct 20 '15 at 20:54
  • You can simulate multiple lines of input, too. Rather than "data", the string becomes, say, `("line1" + System.lineSeparator() + "line2")` – ThatOneCloud Oct 20 '15 at 20:57
0

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();
  • I might try a scan from a file just for the sake of testing it somehow without my interaction, but it might be the answer to my test; I'll be back – 4673_j Oct 20 '15 at 20:51
  • Sorry for the huge delay, I was away; I have sort it out using a txt file as input and I used scannerNextLine() whenever I need it. Thank you for your time. I cannot give you the thick as I don't have enough reputation.. – 4673_j Nov 02 '15 at 21:22