Ant + JUnit = ClassNotFoundExceptions when running tests?

I am trying to run several tests in Ant using JUnit and all my tests do not work with the following stacktrace command:

java.lang.ClassNotFoundException: com.mypackage.MyTestCase 

That doesn't make much sense to me. First, I collect my test cases using <javac> , and then run the <junit> task to run the tests. My build file is as follows:

 <target name="compile.webapp.tests" depends="compile.webapp"> <javac srcdir="${test.java.src.dir}" destdir="${test.java.bin.dir}"> <classpath> <filelist> <file name="${red5.home}/red5.jar"/> <file name="${red5.home}/boot.jar"/> <file name="${bin.dir}/${ant.project.name}.jar"/> </filelist> <fileset dir="${red5.lib.dir}" includes="**/*"/> <fileset dir="${main.java.lib.dir}" includes="**/*"/> <fileset dir="${test.java.lib.dir}" includes="**/*"/> </classpath> </javac> </target> <target name="run.webapp.tests" depends="compile.webapp.tests"> <junit printsummary="true"> <classpath> <filelist> <file name="${red5.home}/red5.jar"/> <file name="${red5.home}/boot.jar"/> <file name="${bin.dir}/${ant.project.name}.jar"/> </filelist> <fileset dir="${red5.lib.dir}" includes="**/*.jar"/> <fileset dir="${main.java.lib.dir}" includes="**/*.jar"/> <fileset dir="${test.java.lib.dir}" includes="**/*.jar"/> <fileset dir="${test.java.bin.dir}" includes="**/*.class"/> </classpath> <formatter type="xml"/> <batchtest todir="${test.java.output.dir}"> <fileset dir="${test.java.bin.dir}" includes="**/*TestCase*"/> </batchtest> </junit> <junitreport> <fileset dir="${test.java.output.dir}" includes="**/*"/> <report todir="${test.java.report.dir}"/> </junitreport> </target> 

This is really strange, I can’t fix it. Am I doing something wrong here? The diagram of my project directory looks something like this:

 ${basedir}/src/test/java # this is "test.java.src.dir" ${basedir}/build/test/java # this is "test.java.bin.dir" ${basedir}/lib/main/java # this is "main.java.lib.dir" ${basedir}/lib/test/java # this is "test.java.lib.dir" ${basedir}/build/test/junit # this is "test.java.output.dir" 

My complete build file is available here: http://pastebin.com/SVnciGKR
My properties file is available here: http://pastebin.com/9LCtNQUq


UPDATE

By changing my goals to look lower, I was able to get everything to work. Unfortunately, I need to manually insert ant -junit.jar and junit.jar into my repository, but it works, so I think this solves it. If someone can help me get rid of the need to embed ant -junit.jar and junit.jar, I would really appreciate it:

 <path id="webapp.tests.path" > <pathelement location="${red5.home}/red5.jar"/> <pathelement location="${red5.home}/boot.jar"/> <pathelement location="${bin.dir}/${ant.project.name}.jar"/> <pathelement path="${red5.lib.dir}"/> <pathelement path="${main.java.lib.dir}"/> <pathelement path="${test.java.lib.dir}"/> </path> <target name="compile.webapp.tests" depends="compile.webapp"> <javac srcdir="${test.java.src.dir}" destdir="${test.java.bin.dir}"> <classpath refid="webapp.tests.path"/> </javac> </target> <target name="run.webapp.tests" depends="compile.webapp.tests"> <junit printsummary="true"> <classpath> <path refid="webapp.tests.path"/> <pathelement location="${test.lib.dir}/ant/ant-junit.jar"/> <pathelement location="${test.lib.dir}/ant/junit-4.8.2.jar"/> <pathelement path="${test.java.bin.dir}"/> </classpath> <formatter type="xml"/> <batchtest todir="${test.java.output.dir}"> <fileset dir="${test.java.bin.dir}" includes="**/*TestCase*"/> </batchtest> </junit> <junitreport todir="${test.java.report.dir}"> <fileset dir="${test.java.output.dir}" includes="**/*"/> <report todir="${test.java.report.dir}"/> </junitreport> <delete file="${test.java.report.dir}/TESTS-TestSuites.xml"/> </target> 

If I do not add banks to the class path, I get errors telling me that junit.jar must be in the class path in order to run the <junit> task. Strange, huh?

+4
source share
3 answers

According to the ANT documentation for the JUnit Task, you need to do one of the following parameters to get the junit tests to run, since there is a dependency on junit.jar that is external to ANT:

Note. You must have junit.jar available. You can do one of the following:

  • Put junit.jar and ant -junit.jar in ANT_HOME / lib.
  • Do not put in ANT_HOME / lib, but instead specify their locations in the CLASSPATH environment variable.
  • Add both JARs to your classpath with -lib.
  • Specify the locations of both JARs using the item in the assembly file.
  • Leave ant -junit.jar at your default location in ANT_HOME / lib, but enable junit.jar in the passed <& JUnit GT ;. (since ANT 1.7)

I checked that # 1 by setting junit.jar to ANT_HOME \ lib worked (ant -junit.jar was already there, ANT 1.8.0), and I did not need to specify junit.jar in the class path for the JUNIT tag. In fact, you took option number 5.

+4
source

I think this is the problem:

 <junit printsummary="true"> <classpath> ... <fileset dir="${test.java.bin.dir}" includes="**/*.class"/> </classpath> 

I believe that classpath should not include .class files directly, only the root of bin dir.

 <junit printsummary="true"> <classpath> ... <pathelement path="${test.java.bin.dir}"/> </classpath> 

A good way to debug this is to quickly build and carefully examine the class path to make sure it is set up the way you expect it to be installed. See Ant command line -verbose and possibly -debug .

I am surprised to set the classpath with just the fileset and file elements instead of pathelement s. Take a look at the Ant manual on Path-like Structures to see what I mean.

Finally, duplication between classes in javac and junit may cause problems in the future. You will probably want to name specific paths or a collection of paths so that you do not duplicate them. See Ant's Guide to References .

+3
source

You need to reference $ {test.java.bin.dir} in the path to the junit task class.

Edit: Hmm, if with "still unsuccessful" you mean the same error, then my next guess is that something should be confused with where the root directory is relative to where the packages start, but to me You need to know more about your directory structure.

0
source

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


All Articles