New to Ant, ClassNotFoundException with JUnit

I scratch my head a bit over this for a while (Googled a bunch, looked at other related SO posts, to no avail). I have a Java program consisting of two files: Logic and Tests . Tests contains about a hundred JUnit tests, and I got a 100% chance of success with the specified tests by calling javac *.java and then java org.junit.runner.JUnitCore Tests . However, when I run my build.xml with a simple ant -verbose test (to keep track of the output, since I'm new to all of 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 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 quite simple. Tests.java , Logic.java , junit-4.10.jar , mysql-connector-java-5.1.18-bin.jar , build.xml , and the referenced .properties file is in the same folder. Java code refers to external files, but they are not related to this particular problem. I do not know if this could be the cause (since I am sure that I currently have no work).

Thanks!

+5
source share
1 answer

You will need to add the directory with Tests.class to the classpath.tests class path (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> 
+3
source

Source: https://habr.com/ru/post/927022/


All Articles