与多用途的testng测试用例共享同一dataprovider,在从excel读取第二个测试用例的数据时,给出Null Pointer异常

时间:2019-06-07 07:06:19

标签: java testng

TestNGclass Test2具有两个必须用同一组数据运行的测试用例,所以我将相同的数据提供者传递给在另一个TestNg类Test1中定义的两个用例。

当我没有从excel传递数据时,它运行良好

public class Test1 {

    static String filepath = new File("").getAbsolutePath();

    @DataProvider(name = "sample")
    public static Object[][] readdata(Method m) throws Exception {
        String name = m.getName();
        System.out.println("NAME===>" + name);
        String[][] locationdata = null;
        String[][] finallocation = null;

        if ("runlocation".equals(name)) {
            locationdata = XLSXReader.getData(filepath + "//src//test//java//com//Myname//dataset//Sample.xlsx", "Sheet1");
            finallocation = new String[locationdata.length - 1][2];
            for (int i = 1; i < locationdata.length; i++) {
                String[] s = locationdata[i];
                for (int j = 0; j < s.length; j++) {
                    finallocation[i - 1][j] = s[j];
                    //  System.out.println(finallocation[i-1][j]);
                }
            }
            return finallocation;
        } else if ("slocation".equals(name)) {
            String[][] secondlocationdata = XLSXReader.getData(filepath + "//src//test//java//com//Myname//dataset//Sample.xlsx", "Sheet2");
            String[][] location = new String[secondlocationdata.length - 1][2];
            for (int i = 1; i < secondlocationdata.length; i++) {
                String[] s = secondlocationdata[i];
                for (int j = 0; j < s.length; j++) {
                    location[i - 1][j] = s[j];
                    //  System.out.println(finallocation[i-1][j]);
                }
            }
            return location;
        }
        return finallocation;
    }
}
public class Test2 {

    static String filepath = new File("").getAbsolutePath();

    @Test(dataProvider = "sample", dataProviderClass = Test1.class)
    public void runlocation(String from, String to) {
        System.out.println("From-->" + from + ",  TO-->" + to);
    }

    @Test(dataProvider = "sample", dataProviderClass = Test1.class)
    public void slocation(String from, String to) {
        System.out.println("@@@@@From-->" + from + ",  @@@@@@TO-->" + to);
    }
}

0 个答案:

没有答案