public T build() { return (T) Proxy.newProxyInstance( mockingClass.getClassLoader(), new Class<?>[]{mockingClass}, new MockeryProxy(mockingClass)); }
@Test public void When_Method_Has_Not_Params_Then_Get_Response() throws Throwable { Method method = Providers.class.getMethod("noParams"); Object[] args = {}; String response = (String) mockeryProxy.invoke(null, method, args); assertThat(response, is(RESPONSE)); }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (method.getDeclaringClass() == Object.class) { return method.invoke(this, args); } InterceptorMetadata interceptor = getInterceptorMetadata. with(mockingClass, method, args); try { applyValidationsToParams(method, args); Type adaptedTypeMethod = interceptor .adaptType(method.getGenericReturnType()); MockeryMetadata mockery = getMockeryMetadata .fromMethod(mockingClass, method, adaptedTypeMethod, args); checkSupportType.from(mockingClass, method, mockery, adaptedTypeMethod); Object response = mockery.legal(); return interceptor.onLegalMock(response); } catch (AssertionError e) { return interceptor.onIllegalMock(e); } }
@Test public void When_Mockery_Params_Pass_Validation_Then_Get_Response() throws Throwable { Method method = Providers.class.getMethod("mockeryPass", String.class, String.class); Object[] args = {"s1", "s2"}; String response = (String) mockeryProxy.invoke(null, method, args); assertThat(response, is(RESPONSE)); }
@Before public void init() { mockeryProxy = new MockeryProxy(Providers.class); }
@Test public void When_Param_Type_Is_Not_Supported_Then_Get_Exception() throws Throwable { Method method = Providers.class.getMethod("notSupportedTypeParam", Object.class); Object[] args = {}; exception.expect(RuntimeException.class); mockeryProxy.invoke(null, method, args); }
@Test public void When_Mockery_Params_Do_Not_Pass_Validation_Then_Get_Assertion_Error() throws Throwable { Method method = Providers.class.getMethod("mockeryNotPass", String.class, String.class); Object[] args = {"s1", "s2"}; try { mockeryProxy.invoke(null, method, args); fail(); } catch (AssertionError e) { assertThat(e.getMessage(), is(RESPONSE_ERROR)); } }
@Test public void When_Method_Type_Is_Not_Supported_Then_Get_Exception() throws Throwable { Method method = Providers.class.getMethod("notSupportedTypeMethod"); Object[] args = {}; exception.expect(RuntimeException.class); mockeryProxy.invoke(null, method, args); }
@Test public void When_No_Mockery_Params_Then_Get_Exception() throws Throwable { Method method = Providers.class.getMethod("noMockery", String.class, Integer.class); Object[] args = {"s1", 1}; try { mockeryProxy.invoke(null, method, args); fail(); } catch (RuntimeException e) { assertThat(e.getMessage(), is("When checking method Providers#noMockery \n" + "No @Mockery annotation for param with position 2 was found.\n" + "To fix it, annotate this param with one.\n")); } }