Data provider mismatch in Selenium with TestNG

前端 未结 2 392
半阙折子戏
半阙折子戏 2021-01-25 05:48

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.

相关标签:
2条回答
  • 2021-01-25 06:21

    I know that a solution is already provided for this question, but there is no explicit discussion about why the exception occurred and what the exception means. The exception message is quite long and maybe even a bit confusing. Lets format the exception message & read it carefully.

    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)user123,
    (java.lang.String)password123,
    (java.lang.String)25.0] 
    at org.testng.internal.reflect.DataProviderMethodMatcher.getConformingArguments(DataProviderMethodMatcher.java:49)...etc.
    

    The MethodMatcherException exception occurs when a data provider does not "match" the test method to which it is mapped. In this case, the mismatch occurs because the arguments provided by the data provider do not match the parameters of the test method.

    The exception message basically says that the test method "testData" requires parameters (String, String, Integer), but the arguments provided by the data provider are (String, String, String).

    Hence, the problem lies in the data provider because it gives a String to Integer age. The data provider uses some library to read the cells in a spreadsheet. It implies that the library code returns cell content as a String. So, you need to convert cell data to the right Java type such as Integer.

    0 讨论(0)
  • 2021-01-25 06:29

    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);
    
    }
    
    0 讨论(0)
提交回复
热议问题