Error: org.testng.TestNGException: Cannot find class in classpath: EmpClass
Asked Answered
S

34

37

When i'm trying to run the test suite, am getting this exception. We are using maven project here and i am done with refreshing, cleaning, reinstalling testNG and then imported the maven projects but then also am getting the same exception. Please suggest any ways that am missing here.

Error console:

org.testng.TestNGException: 
Cannot find class in classpath: EmpClass

at org.testng.xml.XmlClass.loadClass(XmlClass.java:81)
at org.testng.xml.XmlClass.init(XmlClass.java:73)
at org.testng.xml.XmlClass.<init>(XmlClass.java:59)
at org.testng.xml.TestNGContentHandler.startElement(TestNGContentHandler.java:543)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)
at com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(AbstractXMLDocumentParser.java:182)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.emptyElement(XMLDTDValidator.java:766)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(XMLDocumentFragmentScannerImpl.java:1350)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2778)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:649)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(SAXParserImpl.java:333)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:195)
at org.testng.xml.XMLParser.parse(XMLParser.java:39)
at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:17)
at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:10)
at org.testng.xml.Parser.parse(Parser.java:168)
at org.testng.TestNG.initializeSuitesAndJarFile(TestNG.java:311)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:88)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Selfsupport answered 28/8, 2014 at 8:11 Comment(6)
Tell us how did you configure your test cases. We can't help by just looking at exception stacktrace....Servia
I configure test cases by taking the help of this link selftechy.com/2012/01/09/setting-up-testng-with-eclipseSelfsupport
1) I create a new java project 2) Downloaded the TestNG archive (testng-6.8.jar file) 3) Configure the build path for testng-6.8.jarSelfsupport
If you're using Maven, why don't you include TestNG on your Maven POM and with a test scope? From there, add your test classes in your src/test folder?Servia
For me, this problem turned out to be a problem with not using the standard directory layout, so maven wasn't seeing the needed java file when it compiled.Tonus
Sometimes I had the same issue try with "Install jars bson, Compress extend, freemarker, reactive streams, rxjava, sparkcore, testng"Pozzuoli
C
25

I also got the same exception:

I tried the following steps:

  1. In Eclipse> Project > Clean. (The Exception remained)

  2. I ran another project(The testng.xml ran successfully)

After running another project, I ran the project in which I was getting the Exception, and fortunately it worked for me, with no errors.

Cown answered 29/9, 2014 at 9:51 Comment(4)
Was facing Same error. Cleaning the project worked for me.Penniepenniless
After every run of TestNG test, I had to do a mvn clean install and then re-run the test. Otherwise, the error persists.Penniepenniless
In Intellij IDE - File->Invalidate Caches->Invalidate Caches/Restart->Invalidate and RestartThunderhead
2022, it still works. I just rebuild mvn project by navigating Build > Rebuild projectGratianna
G
15

You have to edit your tests Runner xml file. See this example:

 <test name="Employees Test">
               <classes>
                      <class name="packagename.classname">
                       </class>
               </classes>
 </test>

In <class name="packagename.classname"> row you must enter your tests package name then name of class that you want to run.

Thanks and wish you all the best.

Goyette answered 9/9, 2014 at 18:15 Comment(4)
It does not work for me at all. And hasn't any sense, too - if it can't find TestNGException file, how can help exact pointing to test classes?Forester
Oh, sorry, my IntelliJ cannot find the TestNGException itself... Then you have only one problem in your explanations: You cannot point to classes and packages in one test description, it is shown as an error. Your example is OK.Forester
Cool, Good luck :)Goyette
I was facing the same error and it helped!. Thanks, pal :)Lemar
S
14

I faced the similar issue on importing maven with testng project. This was solved by converting again into TestNg project by

right click on eclipse project > TestNG > Convert to TestNG

and replacing the existing testng.xml file with the newly created. Clean and update the maven project with mvn install

Note: take backup of your testng.xml

Susansusana answered 13/5, 2015 at 6:56 Comment(0)
U
9

Project>>Clean and Maven>>Update Project worked for me

Upsilon answered 4/7, 2017 at 10:54 Comment(0)
I
5

Go to Project>Build Path>Configure Build Path>Libraries>Remove Error libraries

After Refresh project and run again program.

Insubstantial answered 11/1, 2016 at 7:3 Comment(0)
E
2

My 2 cents on the problem: I was experiencing the same problem, while some part of my path was in Cyrillic. So check the path to your project for special characters. Once I removed this, the problem was fixed

Eonism answered 26/6, 2017 at 11:39 Comment(0)
D
2

I had the same error. I cleaned the maven project then the problem was solved.

Duster answered 28/11, 2019 at 8:59 Comment(0)
C
1

I also ran into the same problem with TestNG version 6.14.2

However, this was due to a foolish mistake on my end and there is no issue whatsoever with maven, testng or eclipse. What I was doing was -

  1. Run project as "mvn clean"
  2. Select "testng.xml" file and right click on it to run as TestNG suite
  3. This was giving me the same error as reported in the original question
  4. I soon realized that I have not compiled the project and the required class files have not been generated that the TestNG would utilize subsequently
  5. The "target" folder will be generated only after the project is compiled (Run as "mvn build")
  6. If your project is compiled, this issue should not be there.
Convolution answered 19/3, 2018 at 10:37 Comment(0)
F
1

I had the same issue when i was using selenium 3.11.xx dependency however my problem was resolved when i used selenium 3.4.x dependency.

Filberto answered 18/6, 2018 at 18:11 Comment(0)
I
1

If you use IntelliJ IDEA, try the following:

  1. From 'File', select "Invalidate Caches/ Restart" option.
  2. Confirm this action by clicking the "Invalidate and Restart" button.
  3. Then the IDEA will be restarted.
  4. You will then find a fresh copy of your project.
  5. You then need to import/ change project format to the Maven project. IDEA will show you options at the bottom right corner.
  6. After this, you should be able to run your project.
Indicator answered 22/4, 2020 at 11:39 Comment(0)
M
0

I had similar issue and the project had some build errors. I did sudo -R 777 to the project and then I cleaned my project. After that it worked fine.

Hope it helps.

Mew answered 16/10, 2014 at 7:56 Comment(0)
A
0

I had the same error when tried to run my tests in a JSF project.

I´m using Eclipse IDE (kepler). So, I did "project > clean" and then ran the tests again of the same project.

It worked!

Amatol answered 7/1, 2015 at 16:49 Comment(0)
G
0

Note: You have to specify class name along with packages as given below.

<suite name="testNGLearning">
    <test name="simpleTest">
        <classes>
            <class name="testngTests.TestNGSimpleTest" />
            <class name="testngTests.TestMessageUtil" />
        </classes>
    </test>
</suite>
Glazer answered 23/2, 2015 at 8:37 Comment(0)
R
0

Got the IDE working again. The solution is quite funny - and even a re-install, and deleting the IDE does not fix it on windows. What you need to do in that case, is to go to the testNG run configurations - and delete them all. This is for the TestNG XML classpath error on windows OS. I am using spring tools suite 3.6.4.

Make certain that you delete all the configurations, make sure the project can build, and if there are no problems, the test should run now.

Regards -Anthony Toorie

Rhombus answered 25/4, 2015 at 1:24 Comment(0)
C
0

In testng.xml file, remove "." from tag "" class name if you are not using packages.

Coleencolella answered 7/9, 2015 at 9:11 Comment(0)
D
0

include the package name with class. It will remove your name. I also got the same issue but adding the package name with class fixed this issue.

Deannadeanne answered 8/9, 2015 at 12:56 Comment(0)
E
0

For me the problem was very strange.
I have a testng.xml file at the root of my Eclipse project. When I changed the file through Eclipse, it wasn't changing the testng.xml file in my directory.

Turns out it made a copy of the folder into the eclipse workspace..

Hope this helps someone out

Ernestinaernestine answered 16/11, 2015 at 15:36 Comment(0)
R
0

In my opinion if src has test/java/ folders then in build path out location should be like target/test-classes.

If src has main/java/ folders then in build path out location should be like target/classes.

Revel answered 20/1, 2016 at 9:6 Comment(0)
E
0

I came across the same issue stated above today. **setting the class-path again in eclipse ** solved the issue .

Entry answered 29/1, 2016 at 13:13 Comment(0)
P
0

I had no problems with this until recently.

Now I had to move the suite's xml test file into the root of the project I was testing.

It may be that there is a setting to point at the classes I am testing, but I have not found it.

Palumbo answered 27/4, 2016 at 14:22 Comment(0)
M
0

For me, the issue was because I excluded the test from compilation in the POM. My pom.xml looked like this:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <testExcludes>
            <exclude>com/mycompany/app/AppTest.java</exclude>
        </testExcludes>
    </configuration>
</plugin>

After removing the <exclude>, I was able to run the tests in AppTest.java again.

Miscible answered 25/5, 2016 at 17:40 Comment(0)
J
0

In my case I imported maven project as Default existing project in eclipse.After that I imported as maven project.That worked for me.

Johanajohanan answered 16/6, 2016 at 7:16 Comment(0)
A
0

This issue can be resolved by adding TestNG.class package to OnlineStore package as below

  1. Step 1: Build Path > Configure Build Path > Java Build Path > Select "Project" tab
  2. Step 2: Click "add" button and select the project with contains the TestNG source.
  3. Step 3 select TestNG.xml run as "TestNG Suite"
Africa answered 5/8, 2017 at 5:49 Comment(0)
D
0

I had this same problem when I imported my project from one PC to another on IntelliJ IDEA.

Below Solution worked for me:

  1. Import project using Git.
  2. After Importing IDE would prompt you a dialog on the bottom right corner saying "Import as Gradle", you need to select that but make Sure you have set JAVA_HOME and GRADLE(Gradle Home or Gradle wrapper both should work) properly.
  3. Run the build.gradle and make sure all the dependencies are downloaded correctly without any error.
  4. Now move to your Test Case and try to run with TestNG or JUnit and if you have followed above 3 steps properly you should able to run the test case.
Dianetics answered 20/9, 2018 at 12:32 Comment(0)
W
0

After done trying everything that I found on this issue, in eclipse,

I selected my project --> right click --> Run as --> Maven generate-sources

enter image description here

Then I re-ran my TestNG project and it ran perfectly fine without any issues.Hope that helps :)

Wacky answered 21/10, 2018 at 12:0 Comment(0)
D
0

For me it was that I added Skip Tests checkbox/flag in the Maven Build of the Project

Therefore the test classes weren't compiled and then weren't found by TestNG

Deitz answered 24/10, 2018 at 5:16 Comment(0)
F
0

I also had this issue. I'm using IntelliJ IDEA. I was using src/main/java for configuration files and src/test/java for test files.
Because of this testNG couldn't find the class path. To prevent getting that error need to move all the files from src/main/java to src/test/java or vice versa.

Fiacre answered 14/1, 2019 at 13:13 Comment(0)
R
0

You should create package first and then create new TestNG class under that package.

Rosariarosario answered 26/1, 2019 at 6:25 Comment(0)
R
0

Make sure that your class is under src.

Rolfston answered 30/5, 2019 at 10:3 Comment(0)
M
0

Let me share what worked for me:

  1. When we do "mvn clean" - we are cleaning old compiled classes please lgo to ocation "your project directroy -> target " you will find this folder is getting cleaned because that's where MVN places it's compiled artifacts

  2. Since it's already a MAVEN project then go to your project folder and open in command prompt. And issue "mvn clean" and then "mvn test" -> "mvn test" command will place all the compiled files under proper folders and then you can run your tests through TestNG or MAVEN itself.

  3. Make sure you set up "M2_HOME" or "MAVEN_HOME" to your env path , sometime "MAVEN_HOME" doesn't work so add "M2_HOME" as well (google it for this set up)

Above suggestions worked for me so I wanted to share, good luck

Monas answered 2/8, 2019 at 23:24 Comment(0)
D
0

It happens when MAVEN is not installed locally on the system.

I faced the same issue when i cloned a Maven based Project from GIT and then as soon as I executed I got exception "Cannot Find Class In ClassPath : Class Name"

So i did a mvn clean test and found that maven is not installed locally.

After doing sudo apt install maven Maven got installed and i was able to Run the code

Duodiode answered 24/1, 2020 at 7:35 Comment(0)
P
0

I was having the same issue when listener was under src/main/java folder and the base class was under src/test/java, I moved base class under src/main/java and it worked perfectly well enter image description herel

Papiamento answered 6/5, 2021 at 11:53 Comment(0)
Q
0

Same issue here. In my case it was caused by an abstract test class in a separate test package from which all my NG tests were derived. Once I moved that abstract class to the root of the test package, it worked.

Quintal answered 21/10, 2021 at 10:25 Comment(0)
D
0

If you use the Maven project in Eclipse, the following steps will fix your issue.

  1. Right-click on the Project folder(Workplace)
  2. Follow this path and select the Update project option

enter image description here

Discursive answered 22/6, 2023 at 12:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.