我使用Reflections API来调用类运行时并调用其方法。但我看到java.lang.reflect.InvocationTargetException
例外。
我需要调用一个名为 - TestClass的类,并通过在从数据库读取数据后传递包含K,V对数据的HashMap execute
来调用其方法qualityCheckData
。
JDBCDBReader:
public void runSQLQueriesValidate() {
....
try {
Class[] paramHash = new Class[1];
paramHash[0] = HashMap.class;
Class cls = Class.forName("<package name>" + className);
Object obj = cls.newInstance();
Method method;
while(re.next()) {
...
qualityCheckData.put(metaData.getColumnLabel(i).toString(), insertData);
}
method = cls.getDeclaredMethod("execute", paramHash);
try {
Object returnV = method.invoke(obj, qualityCheckData);
if (Boolean.parseBoolean((String) returnV) == true) {
test.log(LogStatus.INFO, "Returned true");
}
}
catch(InvocationTargetException e) {
test.log(LogStatus.FATAL, "InvocationTargetException
seen, please investigate " + e.getCause());
}
catch(Exception e) {
test.log(LogStatus.FATAL, "Exception seen, please investigate " + e + Arrays.asList(e.getStackTrace()).stream().map(Objects::toString).collect(Collectors.joining("\n " )));
}
} catch(Exception e) {
...
}
识别TestClass:
public class TestClassextends BasePage {
public HashMap<String, String> map = new HashMap<String, String>();
public TestClass(){}
public TestClass(ExtentTest test) {
super(test);
}
public boolean execute(HashMap<String, String> map) {
test.log(LogStatus.INFO, "Inside execute");
setMap(map);
return true;
}
public HashMap<String, String> getMap() {
return map;
}
public void setMap(HashMap<String, String> map) {
this.map = map;
}
}
例外:
java.lang.reflect.InvocationTargetException
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
com.test.data.validation.table.JDBCDBReader.runSQLQueriesValidate(JDBCDBReader.java:226)
com.test.data.validation.test.CoreTestcases.Test.connEstablish(Test.java:81)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
org.testng.internal.Invoker.invokeTestMethod(Invoker.java:821)
org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1131)
org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:124)
org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
org.testng.TestRunner.privateRun(TestRunner.java:773) org.testng.TestRunner.run(TestRunner.java:623)
org.testng.SuiteRunner.runTest(SuiteRunner.java:357)
org.testng.SuiteRunner.runSequentially(SuiteRunner.java:352) org.testng.SuiteRunner.privateRun(SuiteRunner.java:310)
org.testng.SuiteRunner.run(SuiteRunner.java:259)
org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
org.testng.TestNG.runSuitesSequentially(TestNG.java:1185)
org.testng.TestNG.runSuitesLocally(TestNG.java:1110) org.testng.TestNG.run(TestNG.java:1018)
org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:283)
org.apache.maven.surefire.testng.TestNGXmlTestSuite.execute(TestNGXmlTestSuite.java:75)
org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:120)
org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:373)
org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:334)
org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:119)
org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:407)
编辑:
处理InvocationTargetException
后,我看到java.lang.NullPointerException
答案 0 :(得分:0)
问题是我没有使用参数化构造器调用getConstructor
。问题已解决