jeudi 23 novembre 2017

InvocationTargetException exceptions seen

I am using reflections API to invoke a class run time and invoke its methods. But am seeing java.lang.reflect.InvocationTargetException exceptions.

I need to invoke a class called - TestClass, and invoke its method execute by passing an HashMap qualityCheckData which contains data in K, V pairs after reading data from database.

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 {
                    method.invoke(obj, qualityCheckData);
                    if (Boolean.parseBoolean((String) returnV) == true) {
                        test.log(LogStatus.INFO, "Returned true");
                    }
            }
            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;
    }
}

Exception:

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)





Aucun commentaire:

Enregistrer un commentaire