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:
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:
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?
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:
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:
Now, the class files could be found, but not read:
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.