7

I'm trying to run Junit test from my ant build.xml file. I read here that you can use a junit.jar file instead of using the .jar that is located in the ant.home/lib directory. This is what I want to do since our Jenkins autobuild set-up has no junit.jar file in his ant lib directory.

Even with the simpliest project, I'm always getting this error that the JUnitTask was not found. If you look at my build.xml file, it is clearly included and used in the junit task.

build.xml :

<project default="all">
    <property name="TALK" value="false" />

    <path id="classpath.base">
    </path>
    <path id="classpath.test">
        <fileset dir="." includes="**/*.jar" />
    </path>

    <target name="compile-test">
        <javac srcdir="src" verbose="${TALK}">
            <classpath refid="classpath.test" />
        </javac>
    </target>

    <target name="test" depends="compile-test">
        <junit>
            <classpath refid="classpath.test" />
            <formatter type="brief" usefile="false" />
            <test name="TestClass" />
        </junit>
    </target>

    <target name="all" depends="test" />
</project>

The small example I made to test things out looks like this :

image

EDIT : Updated based on answer

David
  • 1,101
  • 5
  • 19
  • 38

3 Answers3

11

The junit-Documentation is a bit sparse.
If you have ant-junit.jar outside ANT_HOME/lib you need to define the task yourself (the <taskdef/> is from the FAQ, which is also a bit wrong, because it tells to use class="...", which does not exist as an attribute to <taskdef />):

<path id="classpath.test">
    <fileset dir="." includes="*junit.jar" />
</path>

<taskdef name="junit"
  classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask">
  <classpath refid="classpath.test"/>
</taskdef>

This can be used to check the path:

<pathconvert  property="testoutput" refid="classpath.test"/>
<echo>Path = ${testoutput}</echo>

Documentation:
junit task.


Additional information can be found in this chat transcript

Community
  • 1
  • 1
oers
  • 18,436
  • 13
  • 66
  • 75
  • @David do you have ant-junit.jar int ant_home/lib? You need this lib, too somewhere. See the docs: http://ant.apache.org/manual/Tasks/junit.html – oers Mar 19 '12 at 21:36
  • No I don' but if you read the line it says : 2. Do not put either in ANT_HOME/lib, and instead include their locations in your CLASSPATH environment variable. – David Mar 20 '12 at 12:19
  • @david you can have the ant-junit.jar outside the lib folder, but you have to include it in the classpath definition. You need 2 libs: junit.jar and ant-junit.jar (which makes junit.jar work, so so say) – oers Mar 20 '12 at 12:22
  • When you say, it must be defined it the classpath, do you mean the classpath I use in my build.xml file? If so, look at the changes I've made above, it still doesn't work :( – David Mar 20 '12 at 12:40
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/9090/discussion-between-david-and-oers) – David Mar 20 '12 at 12:51
  • On my GNU/Linux machine I had to append an additional `ant-junit4.jar` => I have added in my `build.xml` the following line : `` => I provide my fix in this post : http://stackoverflow.com/a/41470648/938111 – oHo Jan 04 '17 at 18:25
1

found this elsewhere...

(using Mint, based on Debian)

sudo apt-get install ant-optional
Stevko
  • 4,345
  • 6
  • 39
  • 66
0

My solution is create a CLASSPATH variable in Jenkins System Configure, and add the ant-junit.jar path

tt24
  • 1
  • 5