@Test public void testExceptionMatching() { RuntimeException exception = new RuntimeException(); TestInterface iface = ReplaceIfExceptionMatchingProxy.newProxyInstance( TestInterface.class, supplier, thrown -> thrown.equals(exception)); iface.doSomething(); iface.doSomething(); iface.doSomething(); verify(supplier, times(1)).get(); doThrow(exception).when(delegate).doSomething(); assertThatThrownBy(iface::doSomething).isEqualTo(exception); verify(supplier, times(2)).get(); }
@SuppressWarnings("unchecked") @VisibleForTesting static <T> T newProxyInstance( Class<T> interfaceClass, Supplier<T> delegate, Predicate<Throwable> shouldReplace) { return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class<?>[] {interfaceClass}, new ReplaceIfExceptionMatchingProxy<>(delegate, shouldReplace)); }
ReplaceIfExceptionMatchingProxy extends AbstractInvocationHandler { @SuppressWarnings("unchecked") @VisibleForTesting static <T> T newProxyInstance( Class<T> interfaceClass, Supplier<T> delegate, Predicate<Throwable> shouldReplace) { return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class<?>[] {interfaceClass}, new ReplaceIfExceptionMatchingProxy<>(delegate, shouldReplace)); } }
ReplaceIfExceptionMatchingProxy extends AbstractInvocationHandler { @SuppressWarnings("unchecked") @VisibleForTesting static <T> T newProxyInstance( Class<T> interfaceClass, Supplier<T> delegate, Predicate<Throwable> shouldReplace) { return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class<?>[] {interfaceClass}, new ReplaceIfExceptionMatchingProxy<>(delegate, shouldReplace)); } private  ReplaceIfExceptionMatchingProxy(Supplier<T> delegateFactory, Predicate<Throwable> shouldReplace); }
ReplaceIfExceptionMatchingProxy extends AbstractInvocationHandler { @SuppressWarnings("unchecked") @VisibleForTesting static <T> T newProxyInstance( Class<T> interfaceClass, Supplier<T> delegate, Predicate<Throwable> shouldReplace) { return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class<?>[] {interfaceClass}, new ReplaceIfExceptionMatchingProxy<>(delegate, shouldReplace)); } private  ReplaceIfExceptionMatchingProxy(Supplier<T> delegateFactory, Predicate<Throwable> shouldReplace); static T create(
            Class<T> interfaceClass,
            Supplier<T> delegate,
            Duration minCreationInterval,
            Predicate<Throwable> shouldReplace); }
ReplaceIfExceptionMatchingProxy extends AbstractInvocationHandler { @SuppressWarnings("unchecked") @VisibleForTesting static <T> T newProxyInstance( Class<T> interfaceClass, Supplier<T> delegate, Predicate<Throwable> shouldReplace) { return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class<?>[] {interfaceClass}, new ReplaceIfExceptionMatchingProxy<>(delegate, shouldReplace)); } private  ReplaceIfExceptionMatchingProxy(Supplier<T> delegateFactory, Predicate<Throwable> shouldReplace); static T create(
            Class<T> interfaceClass,
            Supplier<T> delegate,
            Duration minCreationInterval,
            Predicate<Throwable> shouldReplace);  }