简体   繁体   English

想要使用 TestNg 包运行 maven 项目,但在执行项目时出错

[英]Want to run a maven project with TestNg packages but getting error when executing the project

I have started to configure a maven project for selenium java and excluded the following junit dependency from pom.xml as I want to make it with TestNG. I have started to configure a maven project for selenium java and excluded the following junit dependency from pom.xml as I want to make it with TestNG. So included testng dependency instead of it.所以包括 testng 依赖而不是它。

junit dependency junit 依赖

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

pom.xml 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>

Compiled the project (mvn compiled) and the BUILD was successfull but problem happened at the time,when executed the project($ mvn test).Following error was shown at terminal.编译项目(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

So I found the problem exist in AppTest.java file.所以我发现问题存在于 AppTest.java 文件中。 Following were still existed there.那里仍然存在追随者。

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

AppTest.java codes as below 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 );
    }
}

How to get rid of this problem.Do I need to configure the apptest.java for TestNG(I tried to so, but, errors were not gone).Should I include both, testNG and junit?如何摆脱这个问题。我是否需要为 TestNG 配置 apptest.java(我试过了,但是错误没有消失)。我应该同时包括 testNG 和 Z587FEFE304B8E3505DE8295804Z3 吗? If so, then will it create any problem in future?如果是这样,那么它会在未来产生任何问题吗? Please suggest.请建议。

  1. When you removed jUnit dependency then corresponding imports does not work当您删除 jUnit 依赖项时,相应的导入不起作用
  2. Create maven archtype project and remove default App folder structure创建 maven archtype 项目并删除默认 App 文件夹结构
  3. Try to add this configuration inside maven sure fire plugin so that testng.xml can run using mvn test command.尝试在 maven 确保触发插件中添加此配置,以便 testng.xml 可以使用 mvn test 命令运行。

    org.apache.maven.plugins maven-surefire-plugin 3.0.0-M4 testng.xml org.apache.maven.plugins maven-surefire-plugin 3.0.0-M4 testng.xml

You cannot just change one library to another and expect your code will still be working.您不能只是将一个库更改为另一个库,并期望您的代码仍然可以工作。 By removing JUnit from pom.xml you just "switch off" this library from your project.通过从 pom.xml 中删除 JUnit ,您只需从项目中“关闭”这个库。

However having a lib in your pom without having any reference to that lib in your code would not make sense.但是,在您的 pom 中拥有一个 lib 而在您的代码中没有对该 lib 的任何引用是没有意义的。 Like in your case you have a lot of places where your code refer to the classes of JUnit library.就像在您的情况下,您的代码在很多地方都引用了 JUnit 库的类。

What you need to do is to carefully examine where your code utilized JUnit entities and change them to TestNG analogues where possible.您需要做的是仔细检查您的代码在哪里使用了 JUnit 实体,并在可能的情况下将它们更改为 TestNG 类似物。 Where it it not I'm afraid you will have to get rid of that code.如果不是,恐怕您将不得不摆脱该代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM