Datadriven not working using Testng data provider

2019-08-27 15:58发布

问题:

I am trying to read data from excel using apache poi with testng data provider but testng is skipping my test method.

Below is the error am getting

SKIPPED: VerifyLoanDetails
java.lang.RuntimeException: java.lang.NullPointerException

Caused by: java.lang.NullPointerException at com.seleniumhybrid.utils.ExcelUtil.getTestdata(ExcelUtil.java:62) at com.seleniumdata.zmartano.LoanDetails.getdata(LoanDetails.java:50)

Below is my class to read data from excel

public class ExcelUtil{

    static Workbook book;
    static Sheet  sheet;
    public static String Testdata_sheet = Constants.path_TestData;



    public static Object  [][] getTestdata(String sheetname) {



        FileInputStream file = null;
        try{
        file = new FileInputStream(Testdata_sheet);
        }
        catch(FileNotFoundException e) {

            e.printStackTrace();
        }


        try {

            book = WorkbookFactory.create(file);
        }
        catch(InvalidFormatException e ) {

            e.printStackTrace();
        }

        catch (IOException e) {

            e.printStackTrace();

        }


        sheet = book.getSheet(sheetname);

        Object [][]data = new Object[sheet.getLastRowNum()][sheet.getRow(0).getLastCellNum()];
        for (int i=0;i<sheet.getLastRowNum();i++) {

            for(int k=0;k<sheet.getRow(0).getLastCellNum();k++) {

                data[i][k]= sheet.getRow(i+1).getCell(k).toString();

            }


        }

        return data;



    }
}

below is my test class to acess the data

public class LoanDetails extends Configreader{



                  WebDriver driver;     

   @BeforeTest
        public  void beforetest() throws Exception { 

                  driver = Browser.GetBrowser();




       }






    @Test(dataProvider = "getdata")

   public  void VerifyLoanDetails(String username) throws Exception {


    driver.findElement(By.xpath(pro.getProperty("account_xpath"))).click();
    driver.findElement(By.xpath(pro.getProperty("username_id"))).sendKeys(username);


    }


    @DataProvider
    public Object[][] getdata(){

        Object data [][] = ExcelUtil.getTestdata("credentials");


        return data;


    }
}