André Hincu
André Hincu

Reputation: 427

How to make ant recognize junit.jar inside project repository

I have a project that uses JUnit for unit tests. The problem is that when I try to compile and run my project from Eclipse, everything works excellent, but when I try to compile with Ant, I get tons of errors that it does not recognize any function from JUnit (like test()). I copied junit.jar inside the project folder, my classpath is "./", but it still does not work. Anybody has an idea of what I should do to get this done?

Upvotes: 0

Views: 113

Answers (1)

David W.
David W.

Reputation: 107090

You need to make sure the JUnit jar is in your classpath when you compile your test code. You'll also need all the other dependencies, plus your classpath for the classes you previously compiled.

Say this is how you compiled your regular code:

<property name="main.lib.dir"   value="???"/>
<property name="junit.jar"      value="???"/>

<target name="compile"
    description="Compile my regular code">
    <javac srcdir="${main.srcdir}"
        destdir="${main.destir}">
        <classpath path="${main.lib.dir}"/>
    </javac>

Notice I have my a directory that contains all the jars that my code is dependent upon. This is ${main.lib.dir}. Notice that my classes are compiled to the ${main.destdir}. Also notice I have a property pointing to the actual JUnit jar in ${junit.jar}. I don't use that one just yet.

Now to compile my test classes:

<target name="test-compile"
    description="Compile my JUnit tests">
    <javac srcdir="${test.srcdir}"
        destdir="${test.destdir}">
        <classpath path="${main.lib.dir}"/>
        <classpath path="${main.destdir}"/>
        <classpath path="${junit.jar}"/>
    </javac>

Notice that there are now three items in my classpath:

  1. The jars that my compiled code depended upon.
  2. The directory where I compiled my non-test Java code
  3. And the JUnit jar itself.

After you compile your test claasses, you can now use the <junit> task to run your tests:

<junit fork="true"
    includeantruntime="true">
    <formatter .../>
    <batchtest todir="${test.output.dir}"/>
    <classpath="${test.destdir}"/>
</junit>

Upvotes: 1

Related Questions