FreeInputStep extends AbstractStep { @Override public boolean run() { answer = null; printMessageWithDefaultAnswer(); Optional<String> answer = processAnswer(); if (!answer.isPresent()) { return false; } while (!validateAnswerAndShowErrors()) { answer = processAnswer(); if (!answer.isPresent()) { return false; } } this.answer = answer.get(); return true; }  @Override boolean run(); @Override String getAnswer();  }
@Test public void testRun() throws Exception { mockFreeInputStep.setConsoleReader(mockConsoleReader("nan\n-1\n2\n")); assertThat(mockFreeInputStep.run(), is(true)); assertThat(mockFreeInputStep.getAnswer(), equalTo("2")); assertThat(mockFreeInputStep.getAnswerField(), equalTo(2)); assertThat(logger.getWarnMessages(), hasSize(2)); assertThat(logger.getOkMessages(), contains(TEST_MESSAGE)); }
@Test public void testExit() throws Exception { mockFreeInputStep.setConsoleReader(mockConsoleReader("q\n")); assertThat(mockFreeInputStep.run(), is(false)); }
@Test public void testExitAfterInvalidAnswer() throws Exception { mockFreeInputStep.setConsoleReader(mockConsoleReader("n\nq\n")); assertThat(mockFreeInputStep.run(), is(false)); }