运行 没有 XML 以编程方式测试 throw NoSuchMethodError XmlPackage

Running testng programmatically without XML throw NoSuchMethodError XmlPackage

我正在尝试 运行 testng 并获取以下异常。

TestngManager.java

package com.mycopmany.myproduct.test.utils;

import java.util.ArrayList;
import java.util.List;

import org.testng.TestNG;
import org.testng.xml.Parser;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlPackage;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

public class TestngManager {
    private static String lastResult;

    public void run(){
        TestNG testNG = new TestNG();
        Utils.init();

        try {
            XmlSuite suite = new XmlSuite();
            suite.setName("TmpSuite");

            XmlTest test = new XmlTest(suite);
            test.setName("TmpTest");
            List<XmlPackage> packages = new ArrayList<>();
            packages.add(new XmlPackage(".*")); //this line throw exception
            test.setXmlPackages(packages) ;

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

            testNG.setXmlSuites(suites);
        } catch (Exception e) {
            e.printStackTrace();
        }
        testNG.run();

        // TODO: step 1: run ngtest
        // TODO: step 2: locate the result file
        // TODO: step 3: read the result
        lastResult = "TODO: read report.html";
    }

    public String getResult(){
        return lastResult;
    }
}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycopmany.myproduct.tests</groupId>
    <artifactId>myproduct-automation</artifactId>
    <version>0.0.1</version>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>[6.1.1,)</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>[2.44.0,)</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency> 
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-swing-testng</artifactId>
            <version>2.1.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io -->
        <!-- <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>-->
            <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>2.2.1</version>
        </dependency>

        <!--  <dependency>
           <groupId>org.neo4j</groupId>
           <artifactId>neo4j-kernel</artifactId>
           <version>2.2.0</version>
           <type>test-jar</type>
           <scope>test</scope>
        </dependency>
        <dependency>
           <groupId>org.neo4j</groupId>
           <artifactId>neo4j-io</artifactId>
           <version>2.2.0</version>
           <type>test-jar</type>
           <scope>test</scope>
        </dependency>
        -->
    </dependencies>
</project>

错误:

   SEVERE: Servlet.service() for servlet [automation.Report] in context with path [/SERVER_PING] threw exception [Servlet execution threw an exception] with root cause
java.lang.NoSuchMethodError: org.testng.xml.XmlPackage.<init>(Ljava/lang/String;)V
    at com.mycopmany.myproduct.test.utils.TestngManager.run(TestngManager.java:27)
    at automation.Report.doGet(Report.java:35)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:509)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1104)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

看来,问题是由依赖冲突引起的,即依赖assertj-swing-testng版本2.1.1依赖于testng版本5.7,Maven不会自动将其识别为依赖冲突版本 6.14.3.

如果可能,我建议将 pom.xml 中的版本 assertj-swing-testng 更新为更新的版本,例如,您可以使用版本 3.8.0:

    <dependency>
        <groupId>org.assertj</groupId>
        <artifactId>assertj-swing-testng</artifactId>
        <version>3.8.0</version>
    </dependency>

另一种选择是排除嵌套的 testng 依赖关系,但这只对次要版本差异是安全的,因为任何版本不兼容都可能导致依赖关系中断。

如何定位此类问题。在Eclipse IDE中查看pom.xml时,有一个选项卡Dependency Hierarchy,显示嵌套依赖项的树视图,能够过滤它们。