0

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

Business logic:

try {
                    Method method = AccessRulesMethods.class.getDeclaredMethod(name, String.class, BigInteger.class);
                    String parameterName = Arrays.asList(method.getParameters()).get(0).getName();
                    Field inputField = input.getClass().getDeclaredField(parameterName);
                    inputField.setAccessible(true);
                    String parameterValueFromInput = (String) inputField.get(input);
                    AccessRulesMethods accessRulesInstance = beanFactory.getBean(AccessRulesMethods.class);
                    methodOutput = (MethodOutput) method.invoke(accessRulesInstance, parameterValueFromInput, input.getIdBp());
                }catch (InvocationTargetException ite){
                    throw ite.getTargetException();
                }catch (NoSuchMethodException | IllegalAccessException | NoSuchFieldException e) {
                    throw new CoreApplicationException("Errore tecnico: invocazione metodo fallita", ErrorConstant.getLocalErrorKey(ErrorConstant.LOCAL_ERROR_CODE_ERRVISREFL001), HttpStatus.INTERNAL_SERVER_ERROR);
                }

I have a bean with some methods: AccessRulesMethod. I get method names from a table in a db and the I call this methods with reflection in a given order. In order to call these method, first I get the name of the required parameter (the second one is fixed) and then I pass this parameter from the input to the API, also with reflection

  • 1
    You should provide a link to that “another discussion” when you’re referring to it. As far as I know, the `when` must contain a single method invocation to describe the method to mock. You’re having *two* chained method invocations, `getClass()` and `getDeclaredMethod`. You can not alter the behavior of the method `getDeclaredMethod` in class `java.lang.Class` by changing (mocking) the class `AccessRulesMethods`. – Holger Apr 13 '21 at 10:04
  • @Holger thanks for your answer. Here is the link to the discussion I metioned earlier: https://stackoverflow.com/questions/57142466/java-mockito-how-to-mock-uncertain-number-of-parameter-method. You can see the person who asked the question gave acomment in the answer saying this is the correct way. Of course I am missing something. What do you mean exactly by alter the behavior? I want to mock the getDeclaredMethod because is called in my business logic. I am going to add my business logic in another comment. Is it possible to achieve what I want? – Saverio Mirko Viola Apr 13 '21 at 14:41
  • @Holger I edited the question to add some business logic – Saverio Mirko Viola Apr 13 '21 at 14:54
  • 1
    Note how the example in the linked answer consists of a single method call to `getDeclaredMethod`, not containing a `getClass` call. The mocked object must be the `Class` instance on which `getDeclaredMethod` is invoked. “mocking” means “altering the behavior”. You want the behavior of the method to change to `thenReturn(mockMethod)` in the specific scenario. That’s what it is all about. – Holger Apr 13 '21 at 15:00
  • @Holger unfortunately Is not possibile ti mock the class Class... Mockito does not allow this nor powermock... – Saverio Mirko Viola Apr 13 '21 at 17:33
  • That contradicts the Q&A you have linked. When Mockito does not support this, then it’s not possible. – Holger Apr 14 '21 at 07:05
  • @Hoger Indeed is not possible. The link does not provide any example or prove. I tried and I searched on the web and that's not possible – Saverio Mirko Viola Apr 15 '21 at 12:54

0 Answers0