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