FlakyProxy extends DefaultProxyBehavior { public static <T> T newJdkProxy(Object obj, FlakinessFunction flakinessFunction) { Class<?> clazz = obj.getClass(); List<Class<?>> interfaces = ClassUtils.getAllInterfaces(clazz); return (T) java.lang.reflect.Proxy.newProxyInstance( clazz.getClassLoader(), interfaces.toArray(new Class[interfaces.size()]), new FlakyProxy(obj, flakinessFunction)); } private  FlakyProxy(Object obj, FlakinessFunction flakinessFunction); static T newJdkProxy(Object obj, FlakinessFunction flakinessFunction);  }
@Test public void failCountDown() throws Exception { DummyService target = new DummyServiceImpl(); DummyService dummyService = FlakyProxy.newJdkProxy(target, FlakinessFunction.failCountDown(1)); try { dummyService.countWords(parsedPage); fail("UncheckedIOException expected"); } catch (UncheckedIOException e) { assertThat(e.getCause(), instanceOf(IOException.class)); } dummyService.countWords(parsedPage); dummyService.countWords(parsedPage); }
@Test public void newJdkProxy() throws Exception { WikipediaServiceJapiMock target = new WikipediaServiceJapiMock(); WikipediaServiceJapi wikipediaServiceJapi = FlakyProxy.newJdkProxy(target, FlakinessFunction.noFlakiness()); String article = wikipediaServiceJapi.getArticle("42"); assertThat(article, startsWith("{{Dieser Artikel|behandelt das Jahr 42")); assertThat(wikipediaServiceJapi, not(instanceOf(target.getClass()))); }
@Test(expected = UncheckedIOException.class) public void alwaysFail() throws Exception { DummyService target = new DummyServiceImpl(); DummyService dummyService = FlakyProxy.newJdkProxy(target, FlakinessFunction.alwaysFail()); dummyService.countWords(parsedPage); }