nus-cs2113-AY2122S2 / forum

3 stars 2 forks source link

Help for No Such Element Exception in JUnit input testing #64

Open BradenTeo opened 2 years ago

BradenTeo commented 2 years ago

I have a problem with JUnit testing. The code below is one of my tests, which when I run it individually, the test passes, but when I run it with the other tests, there is a NoSuchElementException being thrown.

@Test
void execute_command_expectMessage() throws IOException, InvalidInputException, TimeClashException {
        ByteArrayOutputStream outContent = new ByteArrayOutputStream();
        System.setOut(new PrintStream(outContent));
        ByteArrayInputStream inContent = new ByteArrayInputStream("n".getBytes());
        System.setIn(inContent);

        ArrayList<Task> testArrayList = new ArrayList<>();
        TaskList taskList = new TaskList(testArrayList);

        Ui ui = new Ui();
        Storage storage = new Storage("data/test.json");
        storage.wipeSaveData();
        storage.load(taskList);

        String selection = "all";
        String expectedOutput = "expected output";
        new ClearCommand(selection).execute(taskList, ui, storage);
        assertEquals(expectedOutput, outContent.toString());
}

The error occurs at the 3rd last line:

  new ClearCommand(selection).execute(taskList, ui, storage);

where somehow the System's input cannot be read.

Wondering if anyone has experienced similar issues with handling input in JUnit tests and knows how to solve this.

Thank you.