【问题标题】:Want to run a maven project with TestNg packages but getting error when executing the project想要使用 TestNg 包运行 maven 项目,但在执行项目时出错
【发布时间】:2020-08-08 03:04:17
【问题描述】:

我已经开始为 selenium java 配置一个 maven 项目,并从 pom.xml 中排除了以下 junit 依赖项,因为我想使用 TestNG 来实现。所以包含了 testng 依赖而不是它。

junit 依赖

<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
 </dependency>

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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>MobilePackage</groupId>
  <artifactId>MobileProject</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>MobileProject</name>
  <url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
    <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>3.0.0-M4</version>        
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
  <dependencies>

  <dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.1.0</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.0.0-alpha-5</version>
  </dependency>


  </dependencies>
</project>

编译项目(mvn已编译),BUILD成功,但在执行项目时出现问题($ mvn test)。终端显示以下错误。

[ERROR] /home/PC1/Documents/OfficeProject/MobileProject/src/test/java/MobilePackage/AppTest.java:[3,23] package junit.framework does not exist
[ERROR] /home/PC1/Documents/OfficeProject/MobileProject/src/test/java/MobilePackage/AppTest.java:[4,23] package junit.framework does not exist
.
.
.
[ERROR] /home/PC1/Documents/OfficeProject/MobileProject/src/test/java/MobilePackage/AppTest.java:[11,13] cannot find symbol
[ERROR]   symbol: class TestCase
[ERROR] /home/PC1/Documents/OfficeProject/MobileProject/src/test/java/MobilePackage/AppTest.java:[26,19] cannot find symbol
[ERROR]   symbol:   class Test
[ERROR]   location: class MobilePackage.AppTest

所以我发现问题存在于 AppTest.java 文件中。追随者仍然存在。

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

AppTest.java 代码如下

package MobilePackage;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Unit test for simple App.
 */
public class AppTest 
    extends TestCase
{
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public AppTest( String testName )
    {
        super( testName );
    }

    /**
     * @return the suite of tests being tested
     */
    public static Test suite()
    {
        return new TestSuite( AppTest.class );
    }

    /**
     * Rigourous Test :-)
     */
    public void testApp()
    {
        assertTrue( true );
    }
}

如何摆脱这个问题。我是否需要为 TestNG 配置 apptest.java(我试过了,但是错误没有消失)。我应该同时包含 testNG 和 junit 吗?如果是这样,那么它会在未来产生任何问题吗?请提出建议。

【问题讨论】:

    标签: java maven selenium junit testng


    【解决方案1】:
    1. 当您删除 jUnit 依赖项时,相应的导入不起作用
    2. 创建 maven archtype 项目并删除默认 App 文件夹结构
    3. 尝试在 maven sure fire 插件中添加此配置,以便 testng.xml 可以使用 mvn test 命令运行。

      org.apache.maven.plugins maven-surefire-插件 3.0.0-M4 testng.xml

    【讨论】:

      【解决方案2】:

      您不能只是将一个库更改为另一个库并期望您的代码仍然可以工作。通过从 pom.xml 中删除 JUnit,您只需从项目中“关闭”这个库。

      然而,在你的 pom 中有一个 lib 而在你的代码中没有对该 lib 的任何引用是没有意义的。就像您的情况一样,您的代码在很多地方都引用了 JUnit 库的类。

      您需要做的是仔细检查您的代码在哪里使用了 JUnit 实体,并在可能的情况下将它们更改为 TestNG 类似物。如果不是,恐怕您将不得不摆脱该代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-09
        • 2017-09-05
        • 2016-11-19
        相关资源
        最近更新 更多