package tools.devnull.trugger;

import org.mockito.stubbing.Answer;
import tools.devnull.trugger.reflection.Reflection;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.List;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * @author Marcelo Guimarães
 */
public class AnnotationMock {

  private static final Answer returnType(Class annotationType) {
    return (i) -> annotationType;
  }

  public static <T extends Annotation> T mockAnnotation(Class<T> annotationType) {
    T annotation = mock(annotationType);
    when(annotation.annotationType()).then(returnType(annotationType));
    List<Method> methods = Reflection.reflect().methods()
        .filter(method -> method.getDefaultValue() != null)
        .in(annotationType);
    // maps the methods with default value
    for (Method method : methods) {
      when(Reflection.invoke(method).in(annotation).withoutArgs())
          .thenReturn(method.getDefaultValue());
    }
    return annotation;
  }

}

@@ -29,44 +29,8 @@
 r;%0A%0A
-import org.mockito.stubbing.Answer;%0A
 impo
@@ -77,16 +77,16 @@
 ection;%0A
+
 %0Aimport 
@@ -328,112 +328,8 @@
  %7B%0A%0A
-  private static final Answer returnType(Class annotationType) %7B%0A    return (i) -%3E annotationType;%0A  %7D%0A%0A
   pu
@@ -495,19 +495,22 @@
 hen(
-returnType(
+invocation -%3E 
 anno
@@ -520,17 +520,16 @@
 ionType)
-)
 ;%0A    Li
@@ -720,40 +720,36 @@
     
-for (Method method : methods) %7B%0A
+methods.forEach(method -%3E%0A  
     
@@ -821,16 +821,18 @@
         
+  
 .thenRet
@@ -864,15 +864,10 @@
 e())
+)
 ;
-%0A    %7D
 %0A   

Improve code for annotation mock