1

I am trying to simulate multiple inputs in Java console with the help of Scanner. I need them for Unit testing but its failing only first few values are correctly taken but rest of them are ignored. Please have a look at the following code

String simulatedInput = "1\n" + "2\n" + "James\n" + "Snow\n" + "5\n" + "Mango\n" + "6\n";
InputStream in = new ByteArrayInputStream(simulatedInput.getBytes());
Scanner scanner = new Scanner(in);
int index = 0;
if (scanner.hasNextInt()) {
    index = scanner.nextInt();
}
if (index != 1) {
    System.out.println("#1) Should be 1");
}

if (scanner.hasNextInt()) {
    index = scanner.nextInt();
}

if (index != 2) {
    System.out.println("#2) Should be 2");
}
index = -1;
String value = null;
if (scanner.hasNextLine()) {
    value = scanner.nextLine();
}
if (!"James".equals(value)) {
    System.out.println("#3) Should be 'James' but |" + value + "|");
}
if (scanner.hasNextLine()) {
    value = scanner.nextLine();
}
if (!"Snow".equals(value)) {
    System.out.println("#4) Should be 'Snow' but |" + value + "|");
}

if (scanner.hasNextInt()) {
    index = scanner.nextInt();
}
if (index != 5) {
    System.out.println("#5) Should be 5 but " + index);
}

if (scanner.hasNextLine()) {
    value = scanner.nextLine();
}
if (!"Mango".equals(value)) {
    System.out.println("#6) Should be 'Mango' but |" + value + "|");
}

if (scanner.hasNextInt()) {
    index = scanner.nextInt();
}
if (index != 6) {
    System.out.println("#7) Should be 6 but " + index);
}

System.out.println("*** There should be the only line ***");

and the result is as follows, test # 1 and 2 passed but rest are failing from input that is expected to be James

#3) Should be 'James' but ||
#4) Should be 'Snow' but |James|
#5) Should be 5 but -1
#6) Should be 'Mango' but |Snow|
#7) Should be 6 but 5
*** There should be the only line ***

Please note I cannot change code, as I am unit testing this code, I can only change my simulated input. Can this be fixed?

Response to possible duplicate

This is not a duplicate question, I cannot change the code to consume extra \n, I can only make changes in simulated input only.

halfer
  • 18,701
  • 13
  • 79
  • 158
PHP Avenger
  • 1,512
  • 2
  • 30
  • 58
  • 2
    Possible duplicate of [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Spara Jun 29 '19 at 19:18
  • I can't change code, I can only provide the simulated input – PHP Avenger Jun 29 '19 at 19:18
  • 1
    "*No this is not a duplicate question, I cannot change the code to consume extra \n, I can only make changes in simulated input only*" duplicate explains problem and as extra proposes solutions in code, but if you don't want to change your code then simply don't add `\n` where it is not needed and causes problems. For instance if you have code like `int age = scanner.nextInt(); int name = scanner.nextLine()` then don't provide data as `12\nAdam` but `12 Adam`. – Pshemo Jun 29 '19 at 19:28
  • the tricky thing is it need \n to verify an int but when it reads it, it leaves \n. if it possible for you please do run it and try with the suggested change. it wont work – PHP Avenger Jun 29 '19 at 19:33
  • OK, I see it not. When we change `2\nJames` into `2 James` `nextLine()` would return `" James"` not `"James"`... In that case I am afraid you can't fix it since to get rid of that leading space you would need to change input to `2James` but this would require modifying Scanners delimiter to also consider place between digit and non-digit as valid end point of tokens. But modifying that delimiter is modifying code which you can't do. – Pshemo Jun 29 '19 at 19:43
  • in my mind, you have to read read the JAVADoc of Scanner-class, please. its first sentence starts with "A simple text scanner" and the following documentation describes the different behaviours of several methods. so you will never ever be able to manipulate your unit-test(s) to pass. on the other hand - i think - as unit-tester you are responsible to explain your concerns to the methods/functions/procedures author and to stay in dialog with the author. as unit-tester its not about "yeah all my tests pass"; its about "my tests fit the requirements" - keyword: test driven development (tdd) –  Jun 29 '19 at 20:57

0 Answers0