I have an excel, which contains multiple worksheets in it. Each worksheet contains multiple columns. I have a class like this.
public class dptwo extends dpone {
@Test(dataProviderClass = testdp.class, dataProvider = "dp")
public static void methodone(String a, String b , String c)
{
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
Now I have another class
, in the same package named testdp.java
public class testdp{
@DataProvider(name="dp")
public Object[][] getData(Method m) {
String sheetName = m.getName();
int rows = excel.getRowCount(sheetName);
int cols = excel.getColumnCount(sheetName);
Object[][] data = new Object[rows - 1][cols];
for (int rowNum = 2; rowNum <= rows; rowNum++) {
for (int colNum = 0; colNum < cols; colNum++) {
data[rowNum - 2][colNum] = excel.getCellData(sheetName, colNum, rowNum);
}
}
return data;
}
When I run, I'm getting an error as mentioned below.
[ERROR] [Error] java.lang.NegativeArraySizeException
at temp.dpthree.getData(dpthree.java:18)
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:124)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:74)
at org.testng.internal.MethodInvocationHelper.invokeMethodNoCheckedException(MethodInvocationHelper.java:45)
at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:131)
at org.testng.internal.Parameters.handleParameters(Parameters.java:706)
However:
If I run both the Data provider
and the method from same class it works fine.
I have changed the classname
to make it more meaningful names.