4

How do I make Jenkins find JUnit when building with Ant?

Reading the answers here and here I believe I have a classpath issue, but I am unable to solve it. In my Netbeans project I have a single JUnit test which runs fine in Netbeans. I push the project to my Git repo and Jenkins sees the change and builds the project, but I get a bunch of errors (below) because JUnit is not found.

-do-compile-test:
    [javac] Compiling 1 source file to C:\Jenkins\jobs\demo\workspace\demo\build\test\classes
    [javac] C:\Jenkins\jobs\demo\workspace\demo\test\MainTest.java:6: package org.junit does not exist
    [javac] import org.junit.After;
    [javac]                 ^
    [javac] C:\Jenkins\jobs\demo\workspace\demo\test\MainTest.java:7: package org.junit does not exist
    [javac] import org.junit.AfterClass;
    [javac]  

           ^

JUnit is in my %ANT_HOME%\lib directory (Windows 7 64 bit). The file structure for my Netbeans project is like this:

enter image description here

I have tried to edit the classpath via Ant as discussed here, but I cannot get it working right.

This is my first go with Ant, JUnit, and Jenkins, but I think I'm really close to getting it all working correctly with Netbeans and Git. I will gladly provide more info. Been fighting this for 3 days now, so any assistance would be greatly appreciated.

Community
  • 1
  • 1
ubiquibacon
  • 10,451
  • 28
  • 109
  • 179

3 Answers3

1

I know is some late for the reply, but now we have a solution to this problem...

i have same issue i have a project build by ant, i solved including into the build.xml paths to junit.jar & hamcrest-core.jar

  1. Download junit.jar & hamcrest-core.jar from junit repository in git: https://github.com/junit-team/junit/wiki/Download-and-Install

  2. paste the files in someplace in your jenkins/hudson server

  3. edit your build.xml file add this lines:

    <property name="JUNIT_HOME" value="path_to_folder_contain_junit_&_hamcrest_jars"/>
    <path id="JUnit 4.libraryclasspath">
        <pathelement location="${JUNIT_HOME}/junit.jar"/>
        <pathelement location="${JUNIT_HOME}/hamcrest-core.jar"/>
    </path>
    <path id="projectname.classpath">
        <pathelement location="your_bin_folder"/>
        <path refid="JUnit 4.libraryclasspath"/>
    </path>
    
    <!--in your javac task include the classpath -->
    <javac debug="true" debuglevel="${debuglevel}" destdir="your_bin_folder" source="${source}" target="${target}">
        <src path="src"/>
        <classpath refid="projectname.classpath"/>
    </javac>
    
  4. Commit the file to your svn, git, whatever server.

  5. Try the build. now junit is in your classpath and jenkins can complete the build

Grubhart
  • 1,106
  • 13
  • 19
  • +1 for solving an issue i have. However quick question. for the javac element do you put that into the build.xml or the nbproject/build-impl.xml? also for destdir is that build/classes? Any help greatly appreciated – simgineer Mar 06 '13 at 00:18
  • the build.xml is the ant file to jenkins i work with eclipse then i generate this file and upload to svn repository , destdir is the directory where put your .class files – Grubhart Mar 11 '13 at 19:04
0

You can handle this in NetBeans, without messing with the Ant files yourself.

You should

  • Download the Junit and Hamcrest JAR files, and store them in the lib/ directory of your application
  • In NetBeans, right-click your project, select Properties and then Libraries on the left. Switch to the Compile Tests tab, remove the system wide Junit libs, and add the ones from your lib/ directory using the Add JAR/Folder button.

Make sure you have added the relevant Ant files ([project]/build.xml and [project]/nbproject/build-impl.xml to your VCS.

Then just tell Jenkins where the ant file is (the path to [project]/build.xml in your repo), and which Ant targets to run (usually 'test jar' for a Java SE application).

This solution also has the advantage that it works if you cannot (or do not want to) store separate files on the CI server manually. It has the disadvantage that you ship the libs with your app, instead of using system-wide libs which may be available on the server already.

spirit
  • 441
  • 5
  • 14
0

try providing the complete path of the fie with respect to the current directory so considering ur test results are in nbproject, and the current working directory is demo

Under Publish JUnit results write

nbproject/*.xml
Amey
  • 8,470
  • 9
  • 44
  • 63