DefaultCRest extends CRest { @SuppressWarnings("unchecked") public <T> T build(Class<T> interfaze) throws CRestException { try { InterfaceConfig interfaceConfig = configFactory.newConfig(interfaze); return (T) proxyFactory.createProxy(interfaze.getClassLoader(), new CRestInvocationHandler(interfaceConfig), new Class[]{interfaze}); } catch (Exception e) { throw CRestException.handle(e); } } DefaultCRest(ProxyFactory proxyFactory, RequestExecutor requestExecutor, RequestBuilderFactory requestBuilderFactory, InterfaceConfigFactory configFactory); @SuppressWarnings("unchecked") T build(Class<T> interfaze);  }
@Test public void buildShouldBuildAProxyWithCRestInvocationHandler() throws Exception, IllegalAccessException { TestInterface expected = mock(TestInterface.class); ArgumentCaptor<ObjectMethodsAwareInvocationHandler> objectMethodsAwareInvocationHandlerCaptor = ArgumentCaptor.forClass(ObjectMethodsAwareInvocationHandler.class); when(proxyFactory.createProxy(eq(TestInterface.class.getClassLoader()), objectMethodsAwareInvocationHandlerCaptor.capture(), eq(new Class[]{TestInterface.class}))).thenReturn(expected); when(configFactory.newConfig(TestInterface.class)).thenReturn(config); TestInterface actual = toTest.build(TestInterface.class); assertSame(expected,actual); assertEquals("CRestInvocationHandler", objectMethodsAwareInvocationHandlerCaptor.getValue().getClass().getSimpleName()); assertEquals(config, Classes.getFieldValue(objectMethodsAwareInvocationHandlerCaptor.getValue(), "interfaceConfig")); }
@Test public void buildShouldWrapInCRestExceptionWhenFailure() { RuntimeException e = new RuntimeException(); RuntimeException expected = new RuntimeException(); when(proxyFactory.createProxy(eq(TestInterface.class.getClassLoader()), isA(InvocationHandler.class), eq(new Class[]{TestInterface.class}))).thenThrow(e); mockStatic(CRestException.class); when(CRestException.handle(e)).thenReturn(expected); try { toTest.build(TestInterface.class); fail(); } catch (Exception e1) { assertSame(expected, e1); } }