0

I'm writing a command-line game where the user inputs a command. This is the class for the user-interface:

public class UserInterface {

  private int loops = 0;
  private boolean gameEnded;
  private String inputLine;
  private String[] commands;

  public void gameLoop(){
    Scanner input = new Scanner(System.in);
    gameEnded = false;


    while(!gameEnded){
      loops++;
      System.out.print("> ");
      inputLine = input.nextLine().toLowerCase();
      commands = inputLine.split(" ");
      if(commands[0].equals("quit") || commands[0].equals("exit")){
        gameEnded = true;
        continue;
      }
    }
  }

  @Override
  public String toString(){
    return "Loops: " + loops;
  }


}

I'm trying to test the command quit in Junit by using ByteArrayInputStreams to redirect an input string to System.in similar to the examples here: JUnit: How to simulate System.in testing?

This is my testing class:

public class UITest {

    UserInterface ui;
    private ByteArrayInputStream testIn;
    private final InputStream systemIn = System.in;


    /**
     * Changes system.in into a given string
     * @param data
     */
    private void provideInput(String data) {
        testIn = new ByteArrayInputStream(data.getBytes());
        System.setIn(testIn);
    }
    @Before
    public void setUp() {
        ui = new UserInterface();
    }

    @Test
    public void testQuit(){
        String input = "quit\r\n";
        provideInput(input);
        ui.gameLoop();
        System.out.println(input);

        assertEquals("Loops: 1", ui.toString());

    }

    @Test
    public void testQuitWithSpaces(){
        String input = "apple\r\n";
        provideInput(input);

        ui.gameLoop();
        System.out.println(input);

        assertEquals("Loops: 1", ui.toString());

    }

My first test testQuit(), runs perfectly fine. However, when I run the following test testQuitWithSpaces() I don't even get an infinite while loop, I just get this error:

java.util.NoSuchElementException: No line found

    at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
    at package.UserInterface.gameLoop(UserInterface.java:24)
    at package.UITest.testQuitWithSpaces(AdventureTest.java:54)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)


Process finished with exit code -1
James Wang
  • 49
  • 4
  • 2
    You load up the string 'apple', and then a newline, and then nothing else. You start your question with 'there is supposed to be another line' - nope, there's not, you didn't provide one. Just.. try feeding it 'apple\r\nquit\r\n' and watch what happens. – rzwitserloot Sep 12 '20 at 01:15
  • Hi I just tested it and it did work with two commands. Would that mean I have to start every string with "\r\n"? – James Wang Sep 12 '20 at 01:23
  • No, \n (no need for the \r, even on windows) goes in between 2 commands. It doesn't need to be at the beginning of the first one. – rzwitserloot Sep 12 '20 at 03:07

0 Answers0