FinalTask implements ITask { @Override public void execute() throws TaskExecutionException { try { IObject context = (IObject) this.env.getValue(this.contextFieldName); if (null == context) { return; } List<IAction> actions = (List<IAction>) context.getValue(this.finalActionsFieldName); if (null == actions) { return; } for (IAction action : actions) { try { action.execute(this.env); } catch (ActionExecuteException e) { e.printStackTrace(); } } } catch (InvalidArgumentException | ReadValueException e) { throw new TaskExecutionException("Could not execute final task."); } } FinalTask(final IObject environment); @Override void execute();  }
@Test public void checkTaskExecution() throws Exception { IObject env = mock(IObject.class); IObject context = mock(IObject.class); List<IAction> finalActions = new ArrayList<>(); when(env.getValue(new FieldName("context"))).thenReturn(null).thenReturn(context); when(context.getValue(new FieldName("finalActions"))).thenReturn(null).thenReturn(finalActions); Checker checker = new Checker(); IAction action = new IAction() { @Override public void execute(Object actingObject) throws ActionExecuteException, InvalidArgumentException { checker.setChecked(true); } }; finalActions.add(action); ITask task = new FinalTask(env); task.execute(); task.execute(); task.execute(); assertEquals(checker.isChecked(), true); }
@Test public void checkContinueExecutionOnActionException() throws Exception { IObject env = mock(IObject.class); IObject context = mock(IObject.class); List<IAction> finalActions = new ArrayList<IAction>(); when(env.getValue(new FieldName("context"))).thenReturn(context); Checker checker1 = new Checker(); Checker checker2 = new Checker(); IAction action1 = new IAction() { @Override public void execute(Object actingObject) throws ActionExecuteException, InvalidArgumentException { checker1.setChecked(true); throw new ActionExecuteException("something"); } }; IAction action2 = new IAction() { @Override public void execute(Object actingObject) throws ActionExecuteException, InvalidArgumentException { checker2.setChecked(true); } }; finalActions.add(action1); finalActions.add(action2); when(context.getValue(new FieldName("finalActions"))).thenReturn(finalActions); ITask task = new FinalTask(env); task.execute(); assertEquals(checker1.isChecked(), true); assertEquals(checker2.isChecked(), true); }