0
votes

i have a problem with TestNg, I am using java with Selenium. I have a message from TestNG:

testNG:org.testng.internal.reflect.MethodMatcherException: [public void utils.ExcelDataProvider.test1(java.lang.Object[][],org.testng.ITestContext) throws java.lang.InterruptedException] has no parameters defined but was found to be using a data provider (either explicitly specified or inherited from class level annotation). Data provider mismatch Method: test1([Parameter{index=0, type=[[Ljava.lang.Object;, declaredAnnotations=[]}, Parameter{index=1, type=org.testng.ITestContext, declaredAnnotations=[]}])

This is my @Test

@Test(dataProvider = "test1data")
    public void test1(Object[][] data,ITestContext context) throws InterruptedException {

        ExcelUtils excel = new ExcelUtils(path, 0);
        int rowCount=excel.getRowCount();
        int colCount=excel.getColCount();
        String iter = context.getCurrentXmlTest().getParameter("iterations");
        Execute.execute(data,iter);
}

this is my DataProvider

@DataProvider(name = "test1data")
    public Object[][] getData() {
        Object data[][]=testData(path, 0);
        return data;
    }

the method:

public Object[][] testData(String path, int sheetIndex) {
        ExcelUtils excel = new ExcelUtils(path, sheetIndex);
        int rowCount=excel.getRowCount();
        int colCount=excel.getColCount();

        Object data[][] = new Object[rowCount][colCount]; 

        for(int j=0; j<colCount; j++){
            for(int i=0;i<rowCount ;i++ ){
                String cellData=excel.getCellData(i, j);
//              System.out.println("cellData "+cellData);
                data[j][i] = cellData;
            }
        }
        return data;

    }

If I write

public void test1(Object[] data,ITestContext context) throws InterruptedException {

it works (Object[]) but i need a 2-dimensional array.

Do you know what's wrong?

I'm trying to read the excel and I'd like the first column to be the KEY column and the other columns to be the data per each execution.

enter image description here

Thank you

1

1 Answers

1
votes

An error occurs due to test data that does not match with the test method parameter.

A solution to your problem:

@DataProvider(name = "test1data")
    public Object[][] getData() {
        Object data[][] = testData(path, 0);
        return new Object[][] { { data } };
    }

Reason: DataProvider with return type 2d array use for executing the same test with different data.

To make it clear

public class DP
{
 @DataProvider (name = "data-provider")
 public Object[][] dpMethod(){
 return new Object[][] {{"First-Value"}, {"Second-Value"}};
 }

    @Test (dataProvider = "data-provider")
    public void myTest (String val) {
        System.out.println("Passed Parameter Is : " + val);
    }
}

Result :

PASSED: myTest("First-Value")
PASSED: myTest("Second-Value")