Export to GitHub

mockito - issue #276

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


Posted on Jul 28, 2011 by Happy Dog

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.

Comment #1

Posted on Oct 3, 2011 by Happy Horse

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.

Comment #2

Posted on Oct 7, 2011 by Happy Horse

(No comment was entered for this change.)

Comment #3

Posted on Jan 2, 2012 by Swift Bear

i use annotation reflection on my code, and i cannot test if it is working fine

Status: Duplicate

Labels:
Type-Defect Priority-Medium