gpt4 book ai didi

java - Maven:mvn 测试给出错误 + 无法在类路径中找到类

转载 作者:行者123 更新时间:2023-12-05 04:14:55 25 4
gpt4 key购买 nike

直接从 eclipse IDE 运行 testng.xml,测试套件得到执行,但是从 maven 执行 testng.xml 时出错。

运行 mvn compile,它显示构建成功。但是在运行 mvn test 时出现错误。

在通过 mvn test 运行 maven 脚本时,我收到以下错误:

[INFO]
[INFO] --- maven-compiler-plugin:3.3:testCompile (default-testCompile) @ XXX
--
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.18:test (default-test) @ XXX---
[INFO] No tests to run.
[INFO] Surefire report directory: \XXX\target\surefire-reports
Running TestSuite
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.295 s
[INFO] Finished at: 2015-11-09T17:34:34+05:30
[INFO] Final Memory: 17M/208M

--------------------
-----------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.
18:test (default-test) on project XXX: Exception in provider:
[ERROR] Cannot find class in classpath: com.XXX.tests.XXXX.classtoexecute
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal o
rg.apache.maven.plugins:maven-surefire-plugin:2.18:test (default-test) on project XXX: Exception in provider
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor
.java:212)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor
.java:153)

请帮忙。

下面是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.xx.tests</groupId>
<artifactId>XXX</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18</version>
<configuration>
<forkCount>0</forkCount>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.46.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.5</version>
</dependency>
<dependency>
<groupId>atu.testng</groupId>
<artifactId>reports</artifactId>
<version>5.0</version>
</dependency>
</dependencies>
</project>

TestNg 如下所示:

<

?xml version="1.0" encoding="UTF-8"?>
<suite name="XXXX" parallel="none" >
<listeners>
<listener class-name="atu.testng.reports.listeners.ATUReportsListener"></listener>
<listener class-name="atu.testng.reports.listeners.ConfigurationListener"></listener>
<listener class-name="atu.testng.reports.listeners.MethodListener"></listener>
</listeners>
<test name="test1" >
<classes>
<class name="com.XXX.tests.XXXX.classtoexecute"/>
</classes>
</test>
</suite>

项目结构如下: Project Structure

最佳答案

我创建了一个与您描述的相同的项目结构。按原样复制 pom.xml。以下是我为使其执行所需类而采取的一些步骤。

  1. 我将 testng.xml 直接放在项目的根文件夹中,即与 pom.xml 处于同一级别。
  2. 接下来在testng.xml中我写了

    <classes>
    <class name="com.Local.Tests.TestClass"/>
    </classes>

    检查Java文件开头的包名。

  3. 作为 mvn test 运行。

我可以看到我的 selenium 测试成功执行。

    [INFO] Surefire report directory: D:\workspace\TestngPOM\target\surefire-reports
Running TestSuite
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 18.659 sec

试一试。

我的maven项目结构是这样的。 enter image description here

关于java - Maven:mvn 测试给出错误 + 无法在类路径中找到类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33608934/

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