lundi 12 avril 2021

Mocking java.lang.Class getDeclaredMethod(String name, ...Class args) with Mockito not working

I am trying to mock the getDeclaredMethod on a java .class object of a certain defined java type:

AccessRulesMethods accessRulesMock = mock(AccessRulesMethods.class);
Method mockMethod = mock(Method.class);
when(accessRulesMock.getClass().getDeclaredMethod(param.getValue(), String.class, BigInteger.class)).thenReturn(mockMethod); 

but I get the following exception:

java.lang.NoSuchMethodException: myOwnPackage.AccessRulesMethods.methodName(java.lang.String, java.math.BigInteger)
at java.lang.Class.getDeclaredMethod(Class.java:2130)

Lokking into java.lang.Class at line 2130 it seems that the method is not mocked at all. I found in another discussion here that this is the correct way but with no examples... Anyone knows how can I manage to get what I need?

Thanks a lot, Saverio





Aucun commentaire:

Enregistrer un commentaire