通过TestNG运行测试时获取空指针异常

时间:2014-05-26 13:21:55

标签: java selenium-webdriver testng opencsv

我正在使用CSV阅读器从使用DataProvider的测试功能中获取数据并使用此数据。

TestNG Method

@Test(dataProvider = "regCSVData", dataProviderClass = LoginData.class)
public void testLoginUsingCSVFile(LoginData loginData){
    driver.get(appURL);
    Login login = PageFactory.initElements(driver, Login.class);
    login.loginToFwbm(loginData);
}

LoginData Class

@DataProvider(name = "regCSVData")
public static Object[][] getCSVData() throws IOException {
    CSVReader csvReader = new CSVReader(new FileReader(
            LoginData.class.getResource("/regdata.csv").getPath()));
    List<String[]>dataList = csvReader.readAll();
    Object[][]data = new Object[dataList.size()][1];
    List<LoginData> logList = new ArrayList<LoginData>();
    for (String[] strArray:dataList){
        LoginData loginData = new LoginData();
        loginData.setUserName(strArray[0].trim());
        loginData.setPassword(strArray[1].trim());
        logList.add(loginData);
    }
    for (int i=0; i<data.length;i++){
        for(int j=0;j<data[i].length;j++){
            data[i][j] = logList.get(i);
        }
    }
    csvReader.close();
    return data;
}

CSV File

asdf, pa2
qwerty, pa2
john, pa2

当我使用TestNG运行此测试时,我得到空指针异常


    java.lang.RuntimeException: java.lang.NullPointerException
        at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:161)
        at org.testng.internal.Parameters.handleParameters(Parameters.java:429)
        at org.testng.internal.Invoker.handleParameters(Invoker.java:1383)
        at org.testng.internal.Invoker.createParameters(Invoker.java:1075)
        at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1180)
        at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
        at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
        at org.testng.TestRunner.privateRun(TestRunner.java:767)
        at org.testng.TestRunner.run(TestRunner.java:617)
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
        at org.testng.SuiteRunner.run(SuiteRunner.java:240)
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
        at org.testng.TestNG.run(TestNG.java:1057)
        at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
        at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
        at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
    Caused by: java.lang.NullPointerException
        at com.fwbm.dataobject.LoginData.getCSVData(LoginData.java:49)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
        at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:135)
        ... 20 more

Project Structure Image

enter image description here

2 个答案:

答案 0 :(得分:1)

您必须在&#34;运行配置&#34;中将文件添加到classPath。然后可以使用getRessource()加载该文件;

答案 1 :(得分:1)

使用QAF的最佳方式。在QAF中,您不需要为解析数据提供程序文件提供实现。你很容易使用它。

您可以参考xs

中的文档