@Override public void validateResponse(Object response) { interceptorMetadata.validate(response); Type typeMethod = method.getGenericReturnType(); Type adaptedTypeMethod = interceptorMetadata.adaptType(typeMethod); Object adaptedResponse = interceptorMetadata.adaptResponse(response); MockeryMetadata mockeryMethod = mockeryMetadata .fromMethod(providersClass, method, adaptedTypeMethod, null); checkSupportType.from(providersClass, method, mockeryMethod, adaptedTypeMethod); mockeryMethod.validate(adaptedResponse); }
@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); } }
public InterceptorMetadata with(Class<?> mockingClass, Method method, Object[] args) { Annotation[] annotations = mockingClass.getAnnotations(); List<InterceptorMetadata> interceptorsMetadata = new ArrayList<>(); for (Annotation annotation : annotations) { Interceptor interceptorAnnotation = annotation.annotationType() .getAnnotation(Interceptor.class); if (interceptorAnnotation == null) continue; Metadata metadata = new Metadata(mockingClass, method, args, annotation, method.getGenericReturnType()); Interceptor.Behaviour interceptor = instantiateInterface .from(interceptorAnnotation.value()); interceptorsMetadata.add(new InterceptorMetadata(metadata, interceptor)); } if (interceptorsMetadata.size() > 1) { String message = Messages.multipleInterceptorsFoundOnClass(mockingClass); throw new RuntimeException(message); } if (interceptorsMetadata.isEmpty()) { String message = Messages.noInterceptorsFoundOnClass(mockingClass); throw new RuntimeException(message); } return interceptorsMetadata.get(0); }