简体   繁体   中英

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. So included testng dependency instead of it.

junit dependency

<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>

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.

[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. Following were still existed there.

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

AppTest.java codes as below

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? If so, then will it create any problem in future? Please suggest.

  1. When you removed jUnit dependency then corresponding imports does not work
  2. Create maven archtype project and remove default App folder structure
  3. Try to add this configuration inside maven sure fire plugin so that testng.xml can run using mvn test command.

    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.

However having a lib in your pom without having any reference to that lib in your code would not make sense. Like in your case you have a lot of places where your code refer to the classes of JUnit library.

What you need to do is to carefully examine where your code utilized JUnit entities and change them to TestNG analogues where possible. Where it it not I'm afraid you will have to get rid of that code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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