### Question:
Shell { Command runCommand(final LineReader lineReader, final String prompt) { Command command = null; try { command = this.readCommand(lineReader, prompt); } catch (final Exception e) { this.interpreter.printNormal("Unexpected error: " + e.getMessage() + "\n"); e.printStackTrace(); } if (command != null) { try { this.interpreter.runCommand(command); } catch (final CommandExecutionException e) { this.interpreter.printNormal("Error: " + e.getMessage() + "\n"); } } return command; } Shell(final Interpreter interpreter); void run(final LineReader lineReader, final String prompt); Command readCommand(final LineReader lineReader, final String prompt); void exitShell(); Set<String> getCommands();  }### Answer:
@Test public void runCommand_unknown() throws CommandExecutionException { final Writer writer = new StringWriter(); final Interpreter interpreter = ShellTestUtils.getMockInterpreter(writer); final Interpreter interpreterSpy = Mockito.spy(interpreter); final Shell shell = new Shell(interpreterSpy); final LineReader lineReader = Mockito.mock(LineReader.class); Mockito.when(lineReader.readLine(this.prompt)).thenReturn("unknown", "exit"); final Command command = shell.runCommand(lineReader, this.prompt); assertNotNull(command); assertEquals("unknown", command.getName()); Mockito.verify(interpreterSpy).runCommand(Mockito.any(Command.class)); final String printedResult = writer.toString(); assertTrue(printedResult.startsWith("Error: ")); }
@Test public void runCommand_exceptionDuringReading() throws CommandExecutionException { final Writer writer = new StringWriter(); final Interpreter interpreter = ShellTestUtils.getMockInterpreter(writer); final Interpreter interpreterSpy = Mockito.spy(interpreter); final Shell shell = new Shell(interpreterSpy); final LineReader lineReader = Mockito.mock(LineReader.class); final RuntimeException exception = Mockito.mock(RuntimeException.class); Mockito.when(exception.getMessage()) .thenReturn("This exception is thrown intentionally as part of a unit test"); Mockito.when(lineReader.readLine(this.prompt)).thenThrow(exception); final Command command = shell.runCommand(lineReader, this.prompt); assertNull(command); Mockito.verify(interpreterSpy, Mockito.never()).runCommand(Mockito.any(Command.class)); final String printedResult = writer.toString(); assertTrue(printedResult.startsWith("Unexpected error: " + exception.getMessage())); Mockito.verify(exception).printStackTrace(); }