Ant JUnit ClassNotFoundException

I understand that there are many similar questions, such as this one , however after reading most of them I seem to have the same problem, but for different reasons.

I run the <junit> task inside my Ant build and get a ClassNotFoundException . In most of the similar questions (for example, above), this turned out to be different degrees of the author, simply not setting the JUnit class path correctly. Although this can be very good for me, none of the suggestions on any of these issues worked for me.

The structure of my project:

 MyProject/ src/main/java/ FakeObject.java ...the rest of my Java main source src/test/java FakeObjectUnitTest.java ...the rest of my Java test source bin/main FakeObject.class ...the rest of main Java binaries bin/test FakeObjectUnitTest.class ...the rest of main Java binaries lib/main ...my main dependencies lib/test junit-4.10.jar hamcrest-1.1.jar ...the rest of my test dependencies gen/reports ...where JUnit should be placing all test reports build/ build.xml build.properties 

In src/test/java/FakeObjectUnitTest.java :

 // Package and imports omitted public class FakeObjectUnitTest { @Test public void doSomething() { int x = 5; Assert.assertEquals(x, 5); } } 

My build.xml :

 <project name="" basedir=".."> <!-- Main classpath. --> <path id="main.class.path"> <fileset dir="bin/main"/> </path> <!-- Test classpath. --> <path id="test.class.path"> <fileset dir="bin/test"/> </path> <!-- JUnit classpath. --> <path id="junit.class.path"> <fileset dir="lib/main" includes="*.jar"/> <fileset dir="lib/test" includes="*.jar"/> <path refid="main.class.path"/> <path refid="test.class.path"/> </path> <!-- All previous targets omitted for brevity (such as for compiling, etc.), but I assure you all that they work and compile FakeObject.java/FakeObjectUnitTest.java into FakeObject.class/FakeObjectUnitTest.class respectively, and place them in the correct bin directories. --> <target name="run-junit" depends="compile"> <property name="junit.path" refid="junit.class.path"/> <echo message="JUnit ClassPath is: ${junit.path}"/> <junit printsummary="yes" haltonerror="yes" haltonfailure="yes"> <classpath refid="junit.class.path"/> <formatter type="xml"/> <batchtest todir="gen/reports"> <fileset dir="src/test/java"> <include name="**/*UnitTest*.java"/> </fileset> </batchtest> </junit> </target> </project> 

When I run this target from the command line:

 run-junit: [echo] Conducting unit tests with JUnit. [echo] JUnit ClassPath is: /<path-to-my-project>/MyProject/lib/test/hamcrest-1.1.jar:/<path-to-my-project>/MyProject/lib/test/junit-4.10.jar:/<path-to-my-project>/MyProject/bin/main/com/myproject/client/FakeObject.class:/<path-to-my-project>/MyProject/bin/test/com/myproject/client/FakeObjectUnitTest.class [junit] Running com.myproject.client.FakeObjectUnitTest [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec 

I do this to prove that he sees FakeObjectUnitTest.class in the path of the JUnit class. However, with an error, he fails. When I go to the XML TEST report for this test, I see the following:

 <error message="com.myproject.client.FakeObjectUnitTest" type="java.lang.ClassNotFoundException">java.lang.ClassNotFoundException: com.myproject.client.FakeObjectUnitTest at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:247) </error> 

When I run Ant in verbose mode ( -v ), the only hint I see is a bunch of line-by-line warnings:

Ignoring java.util.zip.ZipException exception: an error occurred while opening the resource for reading zip files x

... where x is the name of the class file.

I found this question about SO, and the problem was that the author added the class files to the class path rather than the JAR (which makes JUnit unhappy), so I tried to remove main.class.path and test.class.path from junit.class/path , and then I get the exception that none of the class files can be found.

Any ideas or thoughts on what is going on? Thanks in advance, and sorry for the -v question here, but I realized that the more details, the better.

+6
source share
3 answers

You need to use for batch structures:
From the manual :

In cases where you need to specify values โ€‹โ€‹of the path type, you can use a nested element. This takes a general view:

  <classpath> <pathelement path="${classpath}"/> <pathelement location="lib/helper.jar"/> </classpath> 

So your solution will look like this:

 <!-- JUnit classpath. --> <path id="junit.class.path"> <fileset dir="lib/main" includes="*.jar"/> <fileset dir="lib/test" includes="*.jar"/> <pathelement location="bin/main"/> <pathelement location="bin/test"/> </path> 
+5
source

Looking at your class path:

 ...:/<path-to-my-project>/MyProject/bin/main/com/myproject/client/FakeObject.class:... 

You cannot put a separate class in this classpath. Instead, put the directory that is at the base of the package hierarchy in the classpath:

 ...:/<path-to-my-project>/MyProject/bin/main:... 
+2
source

make changes to the run-junit target as follows:

  <batchtest todir="gen/reports"> <fileset dir="bin/tests"> <include name="**/*UnitTest*.class"/> </fileset> </batchtest> 
0
source

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


All Articles