MainMethodExceptionHandler { public ActorSystem call(final Callable<ActorSystem> mainMethodBody) { return tryToRunMainMethodBody(checkNotNull(mainMethodBody, "Runnable to be executed")); } private  MainMethodExceptionHandler(final Logger theLogger); static MainMethodExceptionHandler getInstance(final Logger logger); ActorSystem call(final Callable<ActorSystem> mainMethodBody);  }
@SuppressWarnings("ConstantConditions") @Test public void tryToRunNullRunnable() { assertThatNullPointerException() .isThrownBy(() -> underTest.call(null)) .withMessage("The %s must not be null!", "Runnable to be executed") .withNoCause(); }
@Test public void nothingIsLoggedIfRunnableThrowsNoException() { underTest.call(() -> { final int illuminati = 23; final int answer = 42; int i = illuminati; i += answer; return ActorSystem.create(); }); Mockito.verifyZeroInteractions(logger); }
@Test public void exceptionIsReThrownAndLoggedAppropriately() { final String exceptionMessage = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."; final RuntimeException cause = new NullPointerException("Foo must not be null!"); final RuntimeException exception = new IllegalStateException(exceptionMessage, cause); final String expectedLogMessage = MessageFormat.format(LOG_MESSAGE_PATTERN, CLASS_NAME); assertThatExceptionOfType(exception.getClass()) .isThrownBy(() -> underTest.call(() -> { throw exception; })) .withMessage(exceptionMessage) .withCause(cause); Mockito.verify(logger).error(expectedLogMessage, exception); }
@Test public void errorIsReThrownAndLoggedAppropriately() { final String exceptionMessage = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."; final Error error = new OutOfMemoryError(exceptionMessage); final String expectedLogMessage = MessageFormat.format(LOG_MESSAGE_PATTERN, CLASS_NAME); assertThatExceptionOfType(error.getClass()) .isThrownBy(() -> underTest.call(() -> { throw error; })) .withMessage(exceptionMessage) .withNoCause(); Mockito.verify(logger).error(expectedLogMessage, error); }