@Test(expectedExceptions = RuntimeException.class) public void testUncaughtException() { Logger logger = Mockito.mock(Logger.class); Thread thread = new Thread(); thread.setName("foo"); RuntimeException runtimeException = new RuntimeException(); String errorMessage = String.format("Thread %s threw an uncaught exception: %s", thread, runtimeException); Mockito.doThrow(runtimeException).when(logger).error(errorMessage, runtimeException); Thread.UncaughtExceptionHandler uncaughtExceptionHandler = new LoggingUncaughtExceptionHandler(Optional.of(logger)); uncaughtExceptionHandler.uncaughtException(thread, runtimeException); }
@Override public void uncaughtException(Thread t, Throwable e) { this.logger.error(String.format("Thread %s threw an uncaught exception: %s", t, e), e); }
LoggingUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler { @Override public void uncaughtException(Thread t, Throwable e) { this.logger.error(String.format("Thread %s threw an uncaught exception: %s", t, e), e); } }
LoggingUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler { @Override public void uncaughtException(Thread t, Throwable e) { this.logger.error(String.format("Thread %s threw an uncaught exception: %s", t, e), e); } LoggingUncaughtExceptionHandler(Optional<Logger> logger); }
LoggingUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler { @Override public void uncaughtException(Thread t, Throwable e) { this.logger.error(String.format("Thread %s threw an uncaught exception: %s", t, e), e); } LoggingUncaughtExceptionHandler(Optional<Logger> logger); @Override void uncaughtException(Thread t, Throwable e); }
LoggingUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler { @Override public void uncaughtException(Thread t, Throwable e) { this.logger.error(String.format("Thread %s threw an uncaught exception: %s", t, e), e); } LoggingUncaughtExceptionHandler(Optional<Logger> logger); @Override void uncaughtException(Thread t, Throwable e);  }