gpt4 book ai didi

java - 在没有 XML 的情况下以编程方式运行 testng 会抛出 NoSuchMethodError XmlPackage

转载 作者:行者123 更新时间:2023-12-02 11:20:21 24 4
gpt4 key购买 nike

我正在尝试运行 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,其中显示嵌套依赖关系的 TreeView ,能够过滤它们。

关于java - 在没有 XML 的情况下以编程方式运行 testng 会抛出 NoSuchMethodError XmlPackage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49964185/

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