mlinhard / mockito

Automatically exported from code.google.com/p/mockito
0 stars 0 forks source link

Spying object, which have method annotations, fail verify on reflection. #276

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi,

I don't know if there is support, in Mockito, for reflection in a spy object. 
But Mockito seems to be handeling every case I tried except for spying an 
object with an annotated method; and also using reflection to call the 
annotated method.

What steps will reproduce the problem?

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.junit.Test;
import org.mockito.Mockito;

public class MockitoTestCase {

    @Test
    public void test() {
        Bar reciever = Mockito.spy(new Bar());

        Foo invoker = new Foo();

        /*Spy works normally with annotations*/
        //reciever.call("Test123");
        //Mockito.verify(reciever).call("Test123");

        /*
         * Doesn't seem to work when using reflection with annotations.
         * But taking out the annotation will cause this to pass.
         */
        invoker.invoke(reciever);
        Mockito.verify(reciever).call("Test");

    }

    public class Foo {

        public void invoke(Object clazz) {

            for (Method method : clazz.getClass().getDeclaredMethods()) {
                Annotation anno = method.getAnnotation(InvokeThis.class);
                if (anno != null) {
                    try {
                        method.invoke(clazz, "Test");
                    } catch (IllegalArgumentException e) {

                    } catch (IllegalAccessException e) {

                    } catch (InvocationTargetException e) {

                    }
                }
            }
        }
    }

    public class Bar {

        //@InvokeThis
        public void call(String ob) {
            System.out.println("Test Pass!");
        }
    }

    public @interface InvokeThis{}
}

What is the expected output? What do you see instead?
Test should pass. 
"Actually, there were zero interactions with this mock."

What version of the product are you using? On what operating system?
mockito-all-1.8.5.jar from Maven.
Windows XP.

Original issue reported on code.google.com by Ian.Ara...@gmail.com on 28 Jul 2011 at 3:49

GoogleCodeExporter commented 9 years ago
Hi,

Yes we are aware of this issue, our bytecode engine (or the way we are using 
it) does not handle this case. We will certainly be looking for a solution when 
we will investigate bytecode engine change.

Original comment by brice.du...@gmail.com on 3 Oct 2011 at 8:27

GoogleCodeExporter commented 9 years ago

Original comment by brice.du...@gmail.com on 7 Oct 2011 at 8:31

GoogleCodeExporter commented 9 years ago
i use annotation reflection on my code, and i cannot test if it is working fine

Original comment by gcaver...@gmail.com on 2 Jan 2012 at 6:02