gpt4 book ai didi

java.util.ArrayList 无法转换为 org.testng.xml.XmlClass - 运行脚本时抛出此错误

转载 作者:行者123 更新时间:2023-11-29 04:09:26 24 4
gpt4 key购买 nike

我试图在运行时创建一个 testng 文件,这样我就可以只执行我想要的那些类和方法。但是,在运行时我遇到了一个转换错误。请帮助我。

我尝试了所有可能的组合并得出了以下代码

package org.exela.mtclaims;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.testng.TestNG;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlInclude;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

public class CreateTestNGXML extends BaseClass {
public static FileInputStream fis = null;
public static XSSFWorkbook workBook = null;
public static XSSFSheet sheet = null;
public XSSFRow row = null;
public XSSFCell cell = null;
public static int rowCount = 0;

static ArrayList<String> methods = new ArrayList<String>();
static ArrayList<String> classes = new ArrayList<String>();

public static String excelPath = "D:\\Sai\\Documents\\Workspace\\IBM_MTClaims_Automation\\Data Repository\\Data - MT Claims.xlsx";

public static List<XmlInclude> constructIncludes(String... methodNames)
{
List<XmlInclude> includes = new ArrayList<XmlInclude>();
for (String eachMethod : methodNames)
{
includes.add(new XmlInclude(eachMethod));
}
return includes;
}

public static List<XmlClass> constructClasses(ArrayList<String> classNames)
{
List<XmlClass> includes = new ArrayList<XmlClass>();
//String className = "org.exela.mtclaims.";

for (String eachClass : classNames)
{
//String text = className.concat(eachClass);

if(includes.isEmpty())
{
if(!includes.contains("[XmlClass class=org.exela.mtclaims." + eachClass+"]"))
{
includes.add(new XmlClass("org.exela.mtclaims." + eachClass));
}
}
else
{
for (XmlClass xmlClass : includes)
{
if(includes.contains(xmlClass))
{
continue;
}
else
{
includes.add(new XmlClass("org.exela.mtclaims." + eachClass));
}
}
}

/*if (includes.contains("org.exela.mtclaims." + eachClass))
{
includes.add(new XmlClass("org.exela.mtclaims." + eachClass));
}
else
{
includes.add(new XmlClass("org.exela.mtclaims." + eachClass));
}*/
}
return includes;
}

@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
// Creating TestNG Suite
XmlSuite suite = new XmlSuite();
suite.setName("Test Suite");

// Creating TestNG Tests
XmlTest test = new XmlTest(suite);
test.setName("Tests");

// Creating TestNG Classes and Includes to include methods
//List<XmlClass> classesToRun = new List<XmlClass>();
List<XmlClass> classesToRun = new ArrayList<XmlClass>();
List<XmlInclude> methodsToRun = new ArrayList<XmlInclude>();

// Reading data from excel
fis = new FileInputStream(excelPath);
workBook = new XSSFWorkbook(fis);
sheet = workBook.getSheet("TestCases");
rowCount = sheet.getLastRowNum();

// Including methods that should run
for (int i = 2; i < rowCount; i++)
{
if (readDataFromCell(excelPath, "TestCases", "Should Run", i).equals("Yes"))
{
methods.add(readDataFromCell(excelPath, "TestCases", "Methods", i));
}
}

methodsToRun = constructIncludes(new String[] { String.join(",", methods) });

// Including classes that should run
for (int i = 2; i < rowCount; i++)
{
if (readDataFromCell(excelPath, "TestCases", "Should Run", i).equals("Yes")) {
classes.add(readDataFromCell(excelPath, "TestCases", "Classes", i));
}
}

//classesToRun = constructClasses(new String[] { String.join(",", classes) });
classesToRun = constructClasses(classes);
//test.setXmlClasses (Arrays.asList (new XmlClass[] { classesToRun }));
test.setXmlClasses (classesToRun);

// Adding Include to classes
((XmlClass) classesToRun).setIncludedMethods(methodsToRun);


// Adding Classes to tests
test.setXmlClasses((List<XmlClass>) classesToRun);

// Adding suites
List<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add(suite);

// Printing the created suite file
System.out.println("Printing TestNG Suite Xml");
System.out.println(suite.toXml());

// Creating and running the TestNG file
TestNG tng = new TestNG();
tng.setXmlSuites(suites);
tng.run();
}
}

问题在第 129 行,((XmlClass) classesToRun).setIncludedMethods(methodsToRun);。我不明白怎么了。有人可以为此提供解决方案吗?

最佳答案

classesToRun 是一个 XmlClass 的列表,不能转换为单个 XmlClass。您需要遍历列表

for (XmlClass xmlClass : classesToRun) {
xmlClass.setIncludedMethods(methodsToRun);
}

关于java.util.ArrayList 无法转换为 org.testng.xml.XmlClass - 运行脚本时抛出此错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55948610/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com