How with reflection do I pass a method parameter com.ibm.as400.access.AS400JDBCConnection to the following method:
private String test1(Connection con){
return "test1";
}
When I pass from my JUnit to the test1(con) I get the following error. java.lang.NoSuchMethodException: com.action.TestAction.test1(com.ibm.as400.access.AS400JDBCConnection)
I created another method:
private String test2(com.ibm.as400.access.AS400JDBCConnection con){
return "test2";
}
Run with test2(con) and it runs fine. Any input would be greatly appreciated on proper passing into test1 without alteration to the method.
==========================================================
The following is the example link I pulled from: Any way to Invoke a private method?
The following is my JUnit test:
@Test
public void testReturnScreen(){
System.out.println("connection: "+con.getClass());
System.out.println((String) genericInvokMethod(creditCardAction, "test2", 1, con));
System.out.println((String) genericInvokMethod(creditCardAction, "test1", 1, con));
}
public static Object genericInvokMethod(Object obj, String methodName,
int paramCount, Object... params) {
Method method;
Object requiredObj = null;
Object[] parameters = new Object[paramCount];
Class<?>[] classArray = new Class<?>[paramCount];
for (int i = 0; i < paramCount; i++) {
parameters[i] = params[i];
classArray[i] = params[i].getClass();
}
try {
method = obj.getClass().getDeclaredMethod(methodName, classArray);
method.setAccessible(true);
requiredObj = method.invoke(obj, parameters);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return requiredObj;
}