Я получаю сообщение об ошибке wrong number of arguments
для этого кода. Что здесь не так? Мне нужно получить данные из таблицы Excel и предоставить их MyClass
через поставщик данных. В конструкторе используется аннотация @Factory
, потому что мне нужно использовать эти данные для различных методов тестирования в моем классе.
public class MyClass {
HashMap<String, Object> datamap;
@Factory(dataProvider = "dp")
public MyClass(HashMap<String, Object> datamap) {
this.datamap = datamap;
}
-
@DataProvider(name="dp")
public static Object[][] getData() {
ExcelData ed = new ExcelData("filename","sheetname");
Object[][] dataObj = ed.dataObj;
return dataObj;
}
-
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelData {
public Object[][] dataObj = null;
public ExcelData(String filename, String sheetname) {
String data, val;
HashMap<String, Object> testdata;
ArrayList<String> imagelocs;
File file = new File(filename);
try {
FileInputStream fis = new FileInputStream(file);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheet(sheetname);
XSSFRow row = sheet.getRow(0);
int dataCount = row.getLastCellNum()-1;
int varCount = sheet.getLastRowNum()+1;
dataObj = new Object[dataCount][varCount];
for (int j=0; j < dataCount; j++) {
int i = 0;
testdata = new HashMap<>();
imagelocs = new ArrayList<String>();
while (true) {
row = sheet.getRow(i);
try {
data = row.getCell(0).toString();
val = row.getCell(j+1).toString();
} catch (java.lang.NullPointerException e) {
break;
}
if (data.toString().equals("Images")) {
imagelocs.add(val.toString());
} else {
testdata.put(data, val);
}
i++;
}
testdata.put("imagelocs", imagelocs);
dataObj[j][0] = testdata;
}
wb.close();
} catch (FileNotFoundException e) {
System.out.println("File not found");
} catch (IOException e){
System.out.println("Could not read the Excel sheet");
}
}
}
Это трассировка стека:
org.testng.TestNGException:
The factory method class MyClass() threw an exception
at org.testng.internal.FactoryMethod.invoke(FactoryMethod.java:197)
at org.testng.internal.TestNGClassFinder.processFactory(TestNGClassFinder.java:223)
at org.testng.internal.TestNGClassFinder.processMethod(TestNGClassFinder.java:179)
at org.testng.internal.TestNGClassFinder.processClass(TestNGClassFinder.java:171)
at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:121)
at org.testng.TestRunner.initMethods(TestRunner.java:370)
at org.testng.TestRunner.init(TestRunner.java:271)
at org.testng.TestRunner.init(TestRunner.java:241)
at org.testng.TestRunner.<init>(TestRunner.java:192)
at org.testng.remote.support.RemoteTestNG6_12$1.newTestRunner(RemoteTestNG6_12.java:33)
at org.testng.remote.support.RemoteTestNG6_12$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_12.java:66)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:713)
at org.testng.SuiteRunner.init(SuiteRunner.java:260)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:198)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1295)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1273)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1128)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: org.testng.TestNGException:
Cannot instantiate class MyClass
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:30)
at org.testng.internal.FactoryMethod.invoke(FactoryMethod.java:183)
... 21 more
Caused by: java.lang.IllegalArgumentException: wrong number of arguments
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:24)
... 22 more