JUnit tests with Ant and Hudson


Posted by Steven

One of my more persistent current problems is the setup of JUnit tests in our Hudson 3.1.1. After days of try and error, I still couldn't manage to make it work. This article documents my experiments and will hopefully be updated with the solution one day. If you, the reader of this article, came up with a solution, please contact me or leave a comment. Thanks! Please note that the source/xml listings are prototypic.

The first obvious steps are the setup of the hudson job.

The parameter “-v” enables verbose logging and provides much more information about what’s going on and what errors occur. The ant file from the screenshot is this:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project name="server.test" default="test" basedir=".">
  3. <import file="build.xml" />
  4.  
  5. <property name="src" value="src" />
  6. <property name="test.reports" value="junit_output" />
  7. <path id="classPathJUnit">
  8.  
  9. <!—This will add all of our jars into the classpath -->
  10. <fileset dir="../target/target/apf/plugins">
  11. <include name="**/*.jar"/>
  12. </fileset>
  13.  
  14. <!—This will add all of our test classes into the classpath -->
  15. <pathelement path="../server.test/@dot/middleware/" />
  16. </path>
  17.  
  18. <target name="test" depends="build.jars" description="JUnit-Tests durchführen">
  19. <delete dir="${test.reports}" />
  20. <mkdir dir="${test.reports}" />
  21.  
  22. <echo message="Classpath: ${toString:classPathJUnit}"/>
  23.  
  24. <junit printsummary="yes" fork="yes" haltonfailure="no">
  25. <classpath refid="classPathJUnit" />
  26. <formatter type="xml" />
  27.  
  28. <batchtest todir="${test.reports}">
  29. <fileset dir="${src}">
  30. <include name="**/*Test.java" />
  31. </fileset>
  32. </batchtest>
  33. </junit>
  34. <echo message="Ready – test results in ${test.reports}" />
  35. </target>
  36. </project>

Notice the “toString:classPathJUnit”. That will print the classpath used for our tests. As you can see, the resulting test files are placed in /junit_output. To display the results, Hudson needs to be pointed to this directory:

Note that the directory itself is referenced! I’ve seen configurations like “**/*Test.xml”, with which our Hudson couldn’t find a single result file. Running this configuration results in:

  1. [junit] Running middleware.DummyTest
  2. [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
  3.  
  4. BUILD FAILED
  5. D:\Programme\Hudson\jobs\steven_junit_fuer_hudson\workspace\steven_junit_fuer_hudson\server.test\junitTests.xml:37: Process fork failed.
  6. […]
  7. Caused by: java.io.IOException: Cannot run program "D:\CI\jdk1.7.0_21_x86\jre\bin\java.exe": CreateProcess error=206, The filename or extension is too long
  8. at java.lang.ProcessBuilder.start(ProcessBuilder.java:1042)
  9. at java.lang.Runtime.exec(Runtime.java:615)
  10. at org.apache.tools.ant.taskdefs.Execute$Java13CommandLauncher.exec(Execute.java:862)
  11. at org.apache.tools.ant.taskdefs.Execute.launch(Execute.java:481)
  12. at org.apache.tools.ant.taskdefs.Execute.execute(Execute.java:495)
  13. at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeAsForked(JUnitTask.java:1061)
  14. ... 19 more
  15. Caused by: java.io.IOException: CreateProcess error=206, The filename or extension is too long
  16. at java.lang.ProcessImpl.create(Native Method)
  17. at java.lang.ProcessImpl.<init>(ProcessImpl.java:288)
  18. at java.lang.ProcessImpl.start(ProcessImpl.java:133)
  19. at java.lang.ProcessBuilder.start(ProcessBuilder.java:1023)
  20. ... 24 more
  21.  
  22. Total time: 17 seconds
  23. Zeichne Testergebnisse auf.
  24. Finished: FAILURE

The reason for this failure is the huge classpath in combination with fork=”yes” in our build xml. Because I added every one of our library jars, the forked virtual machine cannot be instantiated. So what happens if we remove the fork=”yes” in our build xml?

  1. [junit] Running middleware.DummyTest
  2. [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
  3. [junit] Test middleware.DummyTest FAILED
  4. […]
  5. [echo] Ready - test results in junit_output
  6.  
  7. BUILD SUCCESSFUL
  8. Total time: 4 seconds
  9. Zeichne Testergebnisse auf.
  10. Testergebnisberichte enthalten keine Testergebnisse.
  11. Finished: FAILURE

The reason for the failure of the test can be seen in the resulting xml file. In the Hudson workspace, the folder junit_output has been created and there’s a DummyTest.xml which reads:

  1. <error message="middleware.DummyTest" type="java.lang.ClassNotFoundException">java.lang.ClassNotFoundException: middleware.DummyTest
  2. at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
  3. at java.lang.Class.forName0(Native Method)
  4. at java.lang.Class.forName(Class.java:266)
  5. </error>

Because the compiled test class couldn’t be found, no tests have been executed. Hence, the result file doesn’t have any results. Out of despair I additionally added each test class to the classpath using a filesest:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project name="server.test" default="test" basedir=".">
  3. <import file="build.xml" />
  4.  
  5. <property name="src" value="src" />
  6. <property name="test.reports" value="junit_output" />
  7. <path id="classPathJUnit">
  8. <fileset dir="../target/plugins">
  9. <include name="**/*.jar"/>
  10. </fileset>
  11. <pathelement path="../server.test/@dot/middleware/" />
  12. <fileset dir="../server.test/@dot/middleware/">
  13. <include name="**/*.class"/>
  14. </fileset>
  15. </path>
  16.  
  17. <target name="test" depends="build.jars" description="JUnit-Tests durchführen">
  18. <delete dir="${test.reports}" />
  19. <mkdir dir="${test.reports}" />
  20.  
  21. <echo message="Classpath: ${toString:classPathJUnit}"/>
  22.  
  23. <junit printsummary="yes" haltonfailure="no">
  24. <classpath refid="classPathJUnit" />
  25. <formatter type="xml" />
  26.  
  27. <batchtest todir="${test.reports}">
  28. <fileset dir="${src}">
  29. <include name="**/*Test.java" />
  30. </fileset>
  31. </batchtest>
  32. </junit>
  33. <echo message="Ready - test results in ${test.reports}" />
  34. </target>
  35. </project>

Now, the class files could be found, but not read:

  1. Ignoring Exception java.util.zip.ZipException: error in opening zip file reading resource middleware/DummyTest.class from D:\Programme\Hudson\jobs\steven_junit_fuer_hudson\workspace\steven_junit_fuer_hudson\server.test\@dot\middleware\DummyTest.class
  2. […]
  3. [echo] Ready - test results in junit_output
  4.  
  5. BUILD SUCCESSFUL
  6. Total time: 8 seconds
  7. Zeichne Testergebnisse auf.
  8. Testergebnisberichte enthalten keine Testergebnisse.
  9. Finished: FAILURE

This error is described in an article by Anthony Chaves. Because JUnit expects the test classes to be in a jar file, Anthony changed his build script to build a separate jar for the tests. I don’t want to change our build process in this way, so I stopped searching for a solution to bring Hudson, Ant and JUnit together. Instead, I will try to get the tests running in Jenkins. I will update this article in doing so. Again: If you can help me setup JUnit on Hudson, please contact me! Thanks!

Update:With the help of a coworker, we figured out that the solution to the problem above was indeed to build a separate jar for the tests. We now have two jobs on Hudson: One to build and one to test. That works just fine.

Category: 
Share: 

Comments

Try to long path tool. It is very helpful. 


Yep I tried Long Path Tool it worked for me thanks.


Do not worry if you want to remove the blocked files or too long path files from your system, here I suggest a smooth way. Use “Long path tool” software and keep yourself cool.