ExceptionConverter { public void executeAndConvertException() throws MojoFailureException { try { method.run(); } catch (FailureException fex) { if (fex.getCause() != null) { throw new MojoFailureException(fex.getMessage(), fex.getCause()); } else { throw new MojoFailureException(fex.getMessage()); } } } ExceptionConverter(Runnable method); void executeAndConvertException();  }
@Test void noExceptionShouldRunJustFine() throws MojoFailureException { ExceptionConverter exceptionConverter = new ExceptionConverter(() -> { }); exceptionConverter.executeAndConvertException(); assertThat(exceptionConverter, is(notNullValue())); }
@Test void failureExceptionShouldThrowMojoFailureException() { FailureException failureException = new FailureException("Gurka"); final Executable testMethod = () -> new ExceptionConverter(() -> { throw failureException; }).executeAndConvertException(); final MojoFailureException thrown = assertThrows(MojoFailureException.class, testMethod); assertThat("Unexpected message", thrown.getMessage(), is(equalTo("Gurka"))); }
@Test void failureExceptionShouldKeepCause() { IllegalArgumentException cause = new IllegalArgumentException("not valid"); FailureException failureException = new FailureException("Gurka", cause); final Executable testMethod = () -> new ExceptionConverter(() -> { throw failureException; }).executeAndConvertException(); final MojoFailureException thrown = assertThrows(MojoFailureException.class, testMethod); assertAll( () -> assertThat("Unexpected message", thrown.getMessage(), is(equalTo("Gurka"))), () -> assertThat("Unexpected cause", thrown.getCause(), is(equalTo(cause))) ); }