my DataProvider method dataForPost is returning 2d array of objects and when running the @Test method I get a mismatch error, not sure what is the problem here.
The error missmatch occurs at the test method, I tried to debug but no clue:
FAILED: tests.VisionPointTest.testGen
org.testng.internal.reflect.MethodMatcherException:
[public void tests.VisionPointTest.testGen(java.lang.String,java.lang.String,int,java.lang.String,long,java.lang.String,long,java.lang.String,java.lang.String,java.lang.String,java.lang.String)] 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: testGen([java.lang.String arg0, java.lang.String arg1, int arg2, java.lang.String arg3, long arg4, java.lang.String arg5, long arg6, java.lang.String arg7, java.lang.String arg8, java.lang.String arg9, java.lang.String arg10])
Arguments: [(java.lang.String) ID,(java.lang.String) ARE,(java.lang.String) 125851827,(java.lang.String) 7.84198E+14,(java.lang.String) 468720000,(java.lang.String) male,(java.lang.String) 1982534400,(java.lang.String) Emirati,(java.lang.String) ,(java.lang.String) ALSHEHHI,(java.lang.String) AHMED SAEED ALI]
at org.testng.internal.reflect.DataProviderMethodMatcher.getConformingArguments(DataProviderMethodMatcher.java:46)
at org.testng.internal.Parameters.injectParameters(Parameters.java:918)
at org.testng.internal.invokers.MethodRunner.runInSequence(MethodRunner.java:50)
at org.testng.internal.invokers.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:961)
at org.testng.internal.invokers.TestInvoker.invokeTestMethods(TestInvoker.java:201)
at org.testng.internal.invokers.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:148)
at org.testng.internal.invokers.TestMethodWorker.run(TestMethodWorker.java:128)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.testng.TestRunner.privateRun(TestRunner.java:819)
at org.testng.TestRunner.run(TestRunner.java:619)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:443)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:437)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:397)
at org.testng.SuiteRunner.run(SuiteRunner.java:336)
DataProvider Method:
@DataProvider(name = "data_ids")
public Object[][] dataForPost() throws IOException{
int numFiles = search("C:\\Users\\LP\\eclipse-workspace\\RestAssuredAutomation\\Request_Files");
String names[] = searchnames("C:\\Users\\LP\\eclipse-workspace\\RestAssuredAutomation\\Request_Files");
String jsons[] = new String[numFiles];
for (int i=0; i<=numFiles - 1;i++) {
byte[] b = Files.readAllBytes(Paths.get("Request_Files\\" + names[i]));
String inputrval = new String(b);
jsons[i] = inputrval;
}
String excelPath = "./MyData/IDsData.xlsx";
String sheetName = "Data";
ExcelUtils excel = new ExcelUtils(excelPath,sheetName);
//System.out.println(ExcelUtils.getRowCount());
int rowCount = ExcelUtils.getRowCount();
Object data[][] = new Object[rowCount][11];
for(int i=0; i<=rowCount-1;i++) {
data[i][0] = ExcelUtils.getCellData(i, 0);
data[i][1] = ExcelUtils.getCellData(i, 1);
data[i][2] = ExcelUtils.getCellData(i, 2);
data[i][3] = ExcelUtils.getCellData(i, 3);
data[i][4] = ExcelUtils.getCellData(i, 4);
data[i][5] = ExcelUtils.getCellData(i, 5);
data[i][6] = ExcelUtils.getCellData(i, 6);
data[i][7] = ExcelUtils.getCellData(i, 7);
data[i][8] = ExcelUtils.getCellData(i, 8);
data[i][9] = ExcelUtils.getCellData(i, 9);
data[i][10] = ExcelUtils.getCellData(i, 10);
}
return data;
}
Here is my @Test method:
@Test (dataProvider = "data_ids")
public void testGen(String documentCode, String issuingState, int documentNumber, String optional1, long birthDate, String sex, long expirationDate, String nationality, String optional2, String lastName, String firstName) {
JSONObject request = new JSONObject();
System.out.println(firstName);
}
The exception you see is caused by the data-type mismatch, as the error suggests.
Let's break it down.
Your test method has the following signature:
In the exception you can see it:
There is a diversity in parameter types: Strings, longs, ints.
But your data provider returns an array of strings, see the types near arguments:
So your method waits for strings, longs, and ints, but receives all strings, therefore you have a 'Data provider mismatch'.
To resolve this issue you should either change the types of all test arguments to a 'String', or change the 'getCellData' in your data provider to smth like 'getIntData', 'getLongData' etc (not sure what methods are available to you), or just use Integer.valueOf/Long.valueOf where it is needed.
As a side note, I'd recommend you to create a separate class that will hold all the properties you are reading from the Excel, and then just build objects in your data provider, and pass them the Test. See this answer about polyadic methods.