Java 尝试从主方法运行 TestNG 时无法在类路径中找到类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43094008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:40:57  来源:igfitidea点击:

Cannot find class in classpath when trying to run TestNG from the main method

javamaventestng

提问by dsidler

I run my Selenium Testng tests using Maven, but I am trying to create executable jars, so other team members can simply run an automation suite from their desktop with minimal setup.

我使用 Maven 运行我的 Selenium Testng 测试,但我正在尝试创建可执行的 jars,所以其他团队成员可以简单地从他们的桌面运行自动化套件,只需最少的设置。

So I have added a main "runner" method to create an executable jar.

所以我添加了一个主要的“runner”方法来创建一个可执行的 jar。

public class LaunchTests {

public static void main(String[] args) {
    TestListenerAdapter tla = new TestListenerAdapter();
    TestNG testng = new TestNG();
    List<String> suites = new ArrayList();
    suites.add("testng.xml");//path to xml..
    testng.setTestSuites(suites);
    testng.run();

}

}

}

My best guess is that something needs to be added to the POM.xml?

我最好的猜测是需要将某些内容添加到 POM.xml 中?

When running the testng tests manually from eclipse, or with mvn clean test (Surefire plugin), they run flawlessly. But I get this error when trying to run as a Java program.

当从 Eclipse 手动运行 testng 测试或使用 mvn clean test(Surefire 插件)时,它们运行完美无缺。但是在尝试作为 Java 程序运行时出现此错误。

    Exception in thread "main" org.testng.TestNGException: 
Cannot find class in classpath: package.TestOne
    at org.testng.xml.XmlClass.loadClass(XmlClass.java:81)
    at org.testng.xml.XmlClass.init(XmlClass.java:73)
    at org.testng.xml.XmlClass.<init>(XmlClass.java:59)
    at org.testng.xml.TestNGContentHandler.startElement(TestNGContentHandler.java:548)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.emptyElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(Unknown Source)
    at javax.xml.parsers.SAXParser.parse(Unknown Source)
    at org.testng.xml.XMLParser.parse(XMLParser.java:38)
    at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:16)
    at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:9)
    at org.testng.xml.Parser.parse(Parser.java:172)
    at org.testng.TestNG.initializeSuitesAndJarFile(TestNG.java:302)
    at org.testng.TestNG.run(TestNG.java:991)
    at util.LaunchTests.main(LaunchTests.java:17)

Testng.xml

测试文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite1" parallel="methods" thread-count="1">

    <test name="Chrome Adviser Portal" allow-return-values="true"
        thread-count="1">
        <parameters>
            <parameter name="browser" value="chrome" />
            <parameter name="loginType" value="adviser" />
        </parameters>
        <classes>
            <class name="adviserPortal.AdviserLogin" />
        </classes>
    </test>
</suite>

回答by Anuj Teotia

Yes, you have to add extra pluginsin pom.xml to create a runnable jar .

是的,你必须在 pom.xml 中添加额外的插件来创建一个可运行的 jar 。

<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>StackOverFlow</groupId>
    <artifactId>StackOverFlow</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.stack.JarCreation.Main</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>attached</goal>
                        </goals>
                        <phase>package</phase>
                        <configuration>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                            <archive>
                                <manifest>
                                    <mainClass>com.stack.JarCreation.Main</mainClass>
                                </manifest>
                            </archive>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.9.13.6</version>
        </dependency>
    </dependencies>
</project>

Go to your Project =>Right Click => Run As => Maven build... (which has 3 trailing dots) =>In Goals write clean install

转到您的项目 => 右键单击​​ => 运行方式 => Maven 构建...(有 3 个尾随点) => 在目标中编写全新安装

It will create two jars in target folder.

它将在目标文件夹中创建两个 jar。

The jar which has trailing jar-with-dependencies.jaris the runnable jar.

带有jar-with-dependencies.jar的 jar 是可运行的 jar。

Place it where your chrome driver is and go to the location of jar and run it , it would work.

把它放在你的 chrome 驱动程序所在的位置,然后转到 jar 的位置并运行它,它会工作。

回答by Krishnan Mahadevan

You mentioned that you are able to run the tests from within eclipse via maven surefire plugin and you are not able to run it only when executing your main method.

您提到您可以通过 maven surefire 插件从 eclipse 中运行测试,并且您不能仅在执行 main 方法时运行它。

This can only happen when you class LaunchTestsresides in src/main/javaand your suite xml file testng.xmlis referring to classes within src/test/java. Java classes within src/main/javadonot have visibility into java classes that reside within src/test/java. That explains why TestNG throws the exception Cannot find class in classpath

只有当您的类LaunchTests驻留在src/main/java.xml 文件中并且您的套件 xml 文件testng.xml引用src/test/java. 中的 Java 类src/main/java无法查看驻留在src/test/java. 这就解释了为什么 TestNG 会抛出异常Cannot find class in classpath

And if your LaunchTestsmoves into src/test/javathen by default it wont be included in the uber jar (executable jar) that you are building because test classes are omitted.

如果您LaunchTests进入,src/test/java那么默认情况下它不会包含在您正在构建的 uber jar(可执行 jar)中,因为省略了测试类。

So if you really want to be able to do this, you have two options

所以如果你真的想能够做到这一点,你有两个选择

  1. Move all your test classes from src/test/javainto src/main/javaand then work with it (Surefire plugin configuration in your pom file may need some tweaks to let it know that your test sources is not src/test/javabut src/main/java.
  2. You make use of something like maven jar pluginto create a jar of test classes by referring to here.
  1. 从把你所有的测试类src/test/javasrc/main/java,然后工作,它(Surefire插件在你的POM文件可能需要一些调整,让它知道你的测试源不配置src/test/java,但src/main/java
  2. 您可以maven jar plugin参考此处使用类似创建测试类的 jar 之类的方法。

回答by Dominic Giallombardo

I just resolved this for one of our engineers. He had updated to the latest testng plugin, but we resolved the issue by also updating his build path to a testng 6.11 jar file (he had 6.9.6 before).

我刚刚为我们的一位工程师解决了这个问题。他已经更新到最新的 testng 插件,但我们通过将他的构建路径更新为 testng 6.11 jar 文件(他之前有 6.9.6)解决了这个问题。