I'm trying to build a dynamic method java project for Selenium testing, although I'm coming across the following issue using invoke (java.lang.reflect):
When the invoke call passes arguments for a given method, an error message "java.lang.NoSuchMethodException". If however that came method has it's requirements for arguments removed from the method, and the arguments removed from the invoke call, it works fine.
public class TestProj {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws IOException, InterruptedException {
String testClass = "Class1";
String testMethod = "class1Method3";
Class testMethodArgs[] = {};
Class params[] = {};
Object paramsObj[] = {};
// get the Class
Class thisClass = null;
thisClass = Class.forName(testClass);
// get an instance
Object invClass = null;
invClass = thisClass.newInstance();
// get the method
Method thisMethod = null;
thisMethod = thisClass.getDeclaredMethod(testMethod, testMethodArgs);
// call the method
System.out.println("Starting...");
thisMethod.invoke(invClass, "options");
}
}
class Class1 {
public int class1Method3(String test) {
return 1;
}
}
The below error shows only when thisMethod.invoke(invClass, "options") is passed arguments ("options"). When that "options" part if removed, and the method has it's String test removed, it works fine.
java.lang.NoSuchMethodException: Class1.class1Method3()
at java.lang.Class.getDeclaredMethod(Class.java:2130)
at TestProj.main(TestProj.java:197)
Exception in thread "main" java.lang.NullPointerException
at TestProj.main(TestProj.java:211)
Aucun commentaire:
Enregistrer un commentaire