想要运行带有TestNg软件包的Maven项目,但在执行项目时出现错误

问题描述 投票:0回答:2

我已经开始为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测试)。在终端显示了以下错误。

[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
2个回答
0
投票
  1. 删除jUnit依赖项后,相应的导入将不起作用
  2. 创建Maven原型类型项目并删除默认的App文件夹结构
  3. 尝试在maven sure fire插件中添加此配置,以便可以使用mvn test命令运行testng.xml。

    org.apache.maven.pluginsMaven-surefire-插件3.0.0-M4testng.xml


0
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.