### Question:
StatefulCommand implements Command { @Override public void execute(CommandExecution commandExecution) { final UUID id = commandExecution.getSender().getId(); CommandStep currentState = userStates.getIfPresent(id); Optional<CommandStep> nextState = this.computeNextState(commandExecution, currentState); if (nextState.isPresent()) { this.userStates.put(id, nextState.get()); } else { this.userStates.invalidate(id); } } StatefulCommand(CommandStep initialState); @Override void execute(CommandExecution commandExecution);  }### Answer:
@Test public void whenAllExecuted() { StatefulCommand statefulCommand = new StatefulCommand(first); CommandExecution commandExecution = commandExecution(false); statefulCommand.execute(commandExecution); verify(first).prompt(eq(commandExecution)); statefulCommand.execute(commandExecution); verify(first).execute(eq(commandExecution)); verify(second).prompt(eq(commandExecution)); statefulCommand.execute(commandExecution); verify(second).execute(eq(commandExecution)); verify(third).prompt(eq(commandExecution)); statefulCommand.execute(commandExecution); verify(third).execute(eq(commandExecution)); }
@Test public void whenReset() { StatefulCommand statefulCommand = new StatefulCommand(first); CommandExecution commandExecution = commandExecution(false); statefulCommand.execute(commandExecution); verify(first).prompt(eq(commandExecution)); statefulCommand.execute(commandExecution); verify(first).execute(eq(commandExecution)); verify(second).prompt(eq(commandExecution)); CommandExecution reset = commandExecution(true); statefulCommand.execute(reset); verify(first).prompt(eq(reset)); }
@Test public void whenStepNotPassed() { when(first.execute(any())).thenReturn(false); reset(first); StatefulCommand statefulCommand = new StatefulCommand(first); CommandExecution commandExecution = commandExecution(false); statefulCommand.execute(commandExecution); verify(first).prompt(eq(commandExecution)); reset(first); statefulCommand.execute(commandExecution); verify(first).execute(eq(commandExecution)); verify(first).prompt(eq(commandExecution)); verify(second, times(0)).execute(any()); }