### Question:
ValidationAction { public void validate() { if (!isValidationRunning && onBeforeValidate()) { isValidationRunning = true; loadingStarts.execute(); sessionCommands.getValidateSessionCommand().execute(new ClientSessionCommand.Callback<Collection<DiagramElementViolation<RuleViolation>>>() { @Override public void onSuccess() { loadingCompleted.execute(); onAfterValidation(); } @Override public void onError(final Collection<DiagramElementViolation<RuleViolation>> violations) { loadingCompleted.execute(); onError.accept(violations.toString()); onAfterValidation(); } }); } } ValidationAction(final EditorSessionCommands sessionCommands,
                            final Command loadingStarts,
                            final Command loadingCompleted,
                            final Consumer<String> onError); void validate(); void setBeforeValidation(final BooleanSupplier onBeforeValidation); void setAfterValidation(final Command onAfterValidation);  }### Answer:
@Test public void loadingStarts() { final Command loadingStarts = mock(Command.class); new ValidationAction(editorSessionCommands, loadingStarts, () -> { }, s -> { }).validate(); verify(loadingStarts).execute(); verify(validateSessionCommand).execute(any()); }
@Test public void loadingCompletedOnSuccess() { final Command loadingCompleted = mock(Command.class); new ValidationAction(editorSessionCommands, () -> { }, loadingCompleted, s -> { }).validate(); verify(validateSessionCommand).execute(violationsArgumentCapture.capture()); violationsArgumentCapture.getValue().onSuccess(); verify(loadingCompleted).execute(); }
@Test public void loadingCompletedOnError() { final Command loadingCompleted = mock(Command.class); final Consumer onError = mock(Consumer.class); new ValidationAction(editorSessionCommands, () -> { }, loadingCompleted, onError).validate(); verify(validateSessionCommand).execute(violationsArgumentCapture.capture()); violationsArgumentCapture.getValue().onError(new HashSet<>()); verify(loadingCompleted).execute(); verify(onError).accept(eq("[]")); }
@Test public void preventValidationSpam() { final ValidationAction validationAction = new ValidationAction(editorSessionCommands, () -> { }, () -> { }, s -> { }); validationAction.validate(); validationAction.validate(); validationAction.validate(); verify(validateSessionCommand).execute(violationsArgumentCapture.capture()); violationsArgumentCapture.getValue().onSuccess(); validationAction.validate(); validationAction.validate(); validationAction.validate(); verify(validateSessionCommand, times(2)).execute(violationsArgumentCapture.capture()); violationsArgumentCapture.getValue().onError(new HashSet<>()); validationAction.validate(); validationAction.validate(); validationAction.validate(); verify(validateSessionCommand, times(3)).execute(violationsArgumentCapture.capture()); }