当前位置: 首页 > 知识库问答 >
问题:

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

闻人树
2023-03-14

我正在使用CSV reader从获取数据,并在使用DataProvider的测试函数中使用该数据。

测试方法

@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类

@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;
}
asdf, pa2
qwerty, pa2
john, pa2

    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

项目结构图像

共有1个答案

左丘修齐
2023-03-14

您必须将该文件添加到“运行配置”中的类路径中。然后可以使用getresource()加载文件;

 类似资料: