vendredi 3 avril 2020

How to verify a java.lang.reflect.Method known only from reflection was called on an object

Given a java.lang.reflect.Method object obtained through reflection, how can I test that this Method was called on a given object ?

Here is an example code :

MyClass obj = mock(MyClass.class);
injectInTestedObject(obj, testedObj);

String myMethodIWantToCheckName = retrieveMethodNameBySomeMeans();
Method myMethodIWantToCheck =
  Arrays.asList(MyClass.class.getMethods())
    .stream()
    .filter(method -> method.getName().equals(myMethodIWantToCheckName))
    .findFirst()
    .get();

Now I'd like to check if myMethodIWantToCheck was called on obj.

Basically it's the same functionality as verify from Mockito but without knowing the method at compile time.

Is there any way ? Any library already implementing this ?





Aucun commentaire:

Enregistrer un commentaire