'TestNG Cannot find class in classpath once a new scala class is added to the suite.xml file

Seems like this is a very "popular" question on SO with no definitive way to fix it but I'm asking the question anyway because I can provide some more details. I'm using STS v3.4.0.RELEASE and TestNG v6.8.6.20141201_2240. I also have the ScalaIDE plugin for Eclipse 3.0.4.v-2_11-201407232043.

I've got a simple test (MusicalTest.java in my src/test/java) and its corresponding testsuite.xml file that outlines this class with specific method to be included. I run the xml file as a TestNG test and there are no issues, things run just as expected; my testsuite.xml file looks something like this:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="1" verbose="1" name="Fishy Tests"
    skipfailedinvocationcounts="false" junit="false" parallel="methods"
    data-provider-thread-count="1" annotations="JDK">

    <test verbose="2" name="MusicalTestSuite" annotations="JDK" preserve-order="true">
        <classes>
            <class name="com.rock.and.roll.MusicalTest"/>
        </classes>
    </test>
</suite>

Now I create a new class (in my case, I created a Scala class in the same package as the above test) called MusicalScalaTest.scala. I go ahead and include it in my testsuite.xml as follows:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="1" verbose="1" name="Fishy Tests"
    skipfailedinvocationcounts="false" junit="false" parallel="methods"
    data-provider-thread-count="1" annotations="JDK">

    <test verbose="2" name="MusicalTestSuite" annotations="JDK" preserve-order="true">
        <classes>
            <class name="com.rock.and.roll.MusicalTest"/>
            <class name="com.rock.and.roll.MusicalScalaTest"/>
        </classes>
    </test>
</suite>

Now when I run the xml file as a testNG test, I run into the following error:

org.testng.TestNGException:
Cannot find class in classpath: com.rock.and.roll.MusicalTest
    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 ........ <truncated>
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

Interestingly enough, it doesn't complain about the MusicalScalaTest but complains about the MusicalTest instead. I go ahead and comment out the MusicalScalaTest class in the XML file thinking that that's what's causing the confusion, but on running the testNG test I run into the same error again! After this, I delete the MusicalScalaTest.scala class and delete the "commented" out line from the xml. No dice. I right click on the project and do a Run As -> Mvn Clean. No dice. I restart my IDE, do a Mvn Clean, and Maven Update Project followed by a refresh. No dice. I uninstall the testNG plugin, run a mvn clean test-compile just to be sure and then go through the usual motions of maven update project followed by a refresh. Then I reinstall the testNG IDE and give the same xml a whirl. No dice.

Frankly this smells like a test-ng plugin-based issue that has cached some error that it's not willing to purge. I try to run any other testsuite xml files of the same project and they are all complaining with the same error! So basically all my testSuite xml's are out of commission.

Anybody have any recommendations on next steps? I'm trying to make all this work within the IDE as opposed to shifting to the terminal and running testNG through there. If you think it's not worth it at this point, drop a comment regardless.



Solution 1:[1]

FWIW, what solved the issue for me was upgrading my JDK to the latest (1.7.0_65 as of this writing), making sure that it was the default library picked up by the IDE. Also, after hopping multiple IDEs (STS as referenced above, and the latest Eclipse Luna) I settled on using the Typsafe Scala IDE and NOT the ScalaIDE plugin for eclipse. Don't forget to add this to the pom.xml :

    <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-library</artifactId>
        <version>2.11.0</version>
    </dependency>
    <dependency>
      <groupId>org.scalatest</groupId>
      <artifactId>scalatest_2.10</artifactId>
      <version>2.0</version>
      <scope>test</scope>
    </dependency>

            <plugin>
           <groupId>net.alchim31.maven</groupId>
           <artifactId>scala-maven-plugin</artifactId>
           <version>3.1.5</version>
           <executions>
               <execution>
                   <id>scala-compile-first</id>
                   <phase>process-resources</phase>
                   <goals>
                       <goal>add-source</goal>
                       <goal>compile</goal>
                   </goals>
               </execution>
               <execution>
                   <id>scala-test-compile</id>
                   <phase>process-test-resources</phase>
                   <goals>
                       <goal>testCompile</goal>
                   </goals>
               </execution>
           </executions>
        </plugin>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BSJ