我正在尝试在运行时中创建一个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);。我不明白怎么了有人可以为此提供解决方案吗?

最佳答案

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

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

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

10-10 16:08