Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven: mvn test gives error + Cannot find class in classpath

Running testng.xml directly from eclipse IDE, test suite gets executed but on executing testng.xml from maven gives error.

Running mvn compile, it says BUILD SUCCESS. But on running mvn testit gives error.

On running maven script through mvn test I am receiving below error:

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

Please help.

Below is 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 is as belwo:

<

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

The project structure is as below: Project Structure

like image 234
Abhinav Avatar asked Nov 26 '25 13:11

Abhinav


1 Answers

I created a same project structure as you described. Copied pom.xml as it is. Below are some steps I took to make it execute the required class.

  1. I put testng.xml directly in project's root folder i.e. on same level of pom.xml.
  2. Next in testng.xml I wrote

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

    Check the package name in Java file at the beginning.

  3. Run as mvn test.

I could see my selenium tests executing successfully.

    [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 

Give it a try.

This is how my maven project structure.enter image description here

like image 93
MKay Avatar answered Nov 28 '25 03:11

MKay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!