FunctionUtils { public static <T, U> Function<T, U> wrapFunction(final ThrowingFunction<T, U> functionThatThrows) { return (a) -> { try { return functionThatThrows.apply(a); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } }; } private  FunctionUtils(); static Function<T, U> wrapFunction(final ThrowingFunction<T, U> functionThatThrows); static Consumer<T> wrapConsumer(final ThrowingConsumer<T> consumerThatThrows); static BiConsumer<T,U> wrapBiConsumer(final ThrowingBiConsumer<T,U> consumerThatThrows); static Supplier<T> wrapSupplier(final ThrowingSupplier<T> supplierThatThrows);  }
@Test public void shouldWrapInRuntimeIfFunctionThrows() { final Exception t = new Exception(); try { FunctionUtils.wrapFunction(FunctionUtilsTest::throwIt).apply(t); } catch (Exception ex) { assertThat(ex, instanceOf(RuntimeException.class)); assertSame(t, ex.getCause()); } }
@Test public void shouldNoReWrapInRuntimeIfFunctionThrows() { final Exception t = new RuntimeException(); try { FunctionUtils.wrapFunction(FunctionUtilsTest::throwIt).apply(t); } catch (Exception ex) { assertThat(ex, instanceOf(RuntimeException.class)); assertNull(ex.getCause()); } }