Data provider mismatch in Selenium with TestNG

2019-08-17 06:06发布

I am writing the below code in selenium and below error is showing, please let me know where is the issue.

import org.testng.annotations.DataProvider; import org.testng.annotations.Test;

public class testngexcel {

    public static ExcelReader excel = null;
    @Test(dataProvider = "newdata")
    public void testData(String username, String password, Integer age) {
        System.out.println(username + " - " + password + " - " + age);
    }

    @DataProvider(name = "newdata")
    public static Object[][] getData() {

            if (excel == null) {
                excel = new ExcelReader("C:\\Users\\Anjali.Nautiyal\\Desktop\\selenium\\testngdata.xlsx");
            }

            String sheetName = "login";
            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;

Error:

FAILED: testData org.testng.internal.reflect.MethodMatcherException: Data provider mismatch Method: testData([Parameter{index=0, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=1, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=2, type=java.lang.Integer, declaredAnnotations=[]}]) Arguments: [(java.lang.String)anjali,(java.lang.String)pa,(java.lang.String)25.0] at org.testng.internal.reflect.DataProviderMethodMatcher.getConformingArguments(DataProviderMethodMatcher.java:49) at org.testng.internal.Invoker.injectParameters(Invoker.java:1293) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1187) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) at org.testng.TestRunner.privateRun(TestRunner.java:744) at org.testng.TestRunner.run(TestRunner.java:602) at org.testng.SuiteRunner.runTest(SuiteRunner.java:380) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340) at org.testng.SuiteRunner.run(SuiteRunner.java:289) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301) at org.testng.TestNG.runSuitesLocally(TestNG.java:1226) at org.testng.TestNG.runSuites(TestNG.java:1144) at org.testng.TestNG.run(TestNG.java:1115) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)

1条回答
Bombasti
2楼-- · 2019-08-17 06:41

The only problem I see is this, You are reading from excel so may be all the values are coming in String, unless you are converting that to Integer. However, in your test you expect third argument age to be Integer

Changing type to String should resolve the issue

@Test(dataProvider = "newdata")
public void testData(String username, String password, String age) {
        System.out.println(username + " - " + password + " - " + age);
}

The following code would raise the same error.

@DataProvider(name = "newdata")
public static Object[][] getData() {
    return new Object[][]{
            {"20"},
            {"30"}
    };
}

@Test(dataProvider = "newdata")
public void testData(Integer age) {

    System.out.println(age);

}
查看更多
登录 后发表回答