New to Ant, ClassNotFoundException with JUnit

ant, java, junit

Solution

You will need to add the directory with the `Tests.class` to the `classpath.tests` classpath (which is `${basedir}` in your setup)

Try:

<path id="classpath.test"> 
  <pathelement location="${basedir}/mysql-connector-java-5.1.18-bin.jar" /> 
  <pathelement location="${basedir}/junit-4.10.jar"/> 
  <pathelement location="${basedir}" /> 
  <path refid="classpath.base" /> 
</path>

Problem

I've been scratching my head over this for a while now (Googled a bunch, looked through other related SO posts to no avail). I have a Java program comprised of two files, `Logic` and `Tests`. `Tests` contains about a hundred JUnit tests, and I've gotten 100% success rate with said tests by calling `javac *.java` followed by `java org.junit.runner.JUnitCore Tests`. However when I run my `build.xml` with a simple `ant -verbose test` (in order to follow the output since I'm new to all this), I get the following output: ``` [junit] Testsuite: Tests [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec [junit] [junit] Null Test: Caused an ERROR [junit] Tests [junit] java.lang.ClassNotFoundException: Tests [junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:247) [junit] at java.lang.Class.forName0(Native Method) [junit] at java.lang.Class.forName(Class.java:247) [junit] [junit] [junit] Test Tests FAILED BUILD SUCCESSFUL ``` My `build.xml` is as follows: ``` <project name="ETL_Automation" default="test" basedir="."> <path id="classpath.base"> </path> <path id="classpath.test"> <pathelement location="${basedir}/mysql-connector-java-5.1.18-bin.jar" /> <pathelement location="${basedir}/junit-4.10.jar"/> <path refid="classpath.base" /> </path> <target name="compile"> <javac srcdir="${basedir}"> <classpath refid="classpath.test"/> </javac> </target> <target name="test" depends="compile"> <junit fork="no"> <classpath refid="classpath.test" /> <formatter type="brief" usefile="false" /> <batchtest> <fileset dir="${basedir}/" includes="Tests.class" /> </batchtest> </junit> </target> <target name="clean" depends="test"> <delete> <fileset dir="${basedir}" includes="*.class"/> </delete> </target> ``` The directory structure is pretty straightforward. `Tests.java`, `Logic.java`, `junit-4.10.jar`, `mysql-connector-java-5.1.18-bin.jar`, `build.xml`, and a referenced `.properties` file are all in the same folder. The java code references external files but those are unrelated to this particular issue. I don't know if the classpath could be the cause of this issue (as I'm pretty convinced what I currently have doesn't work). Thanks!

Original source