I am using CSV reader to fetch the data from and using this data in a test function using 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
When I run this test using TestNG, I am getting Null Pointer exception
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
regdata.csv
is not found. – Jens