0

I am using Apache Ant to compile and run JUnit tests. At the moment, it is working on environments where the ant JUnit library is installed as a part of Ant, but not on other environments. I want to fix this. I have tried including the jars needed(junit-4.12.jar and hamcrest-core-1.3.jar, if I understand it correctly) but it is not working. The relevant part of my build.xml is as follows:

  <property name="build.path" location="build/src"/>
  <property name="build.test.path" location="build/test"/>
  <property name="lib.path.rel" value="lib"/>
  <property name="junit.file" value="${lib.path.rel}/junit/junit-4.12.jar"/>
  <property name="hamcrest.file" value="${lib.path.rel}/junit/hamcrest-core-1.3.jar"/>

  <path id="junit.class.path">
    <pathelement location="${junit.file}"/>
    <pathelement location="${hamcrest.file}"/>
    <pathelement location="${build.path}"/>
  </path>

  <target name="test-compile" depends="test-clean">
    <mkdir dir="${build.test.path}"/>

    <javac srcdir="${test.path}"
           destdir="${build.test.path}"
           includeantruntime="false">
      <classpath refid="junit.class.path" />
    </javac>
  </target>

  <target name="test" depends="test-compile">
    <junit>
      <classpath>
        <path refid="junit.class.path"/>
        <pathelement location="${build.test.path}"/>
      </classpath>
      <batchtest>
        <fileset dir="${build.test.path}">
          <include name="**/*Test*"/>
        </fileset>
      </batchtest>
      <formatter type="brief" usefile="false"/>
    </junit>
  </target>

The task test-compile successfully builds the tests.
The task test, however, results in the following error:
Problem: failed to create task or type junit Cause: the class org.apache.tools.ant.taskdefs.optional.junit.JUnitTask was not found.

I am using CentOS 7 with Ant 1.9.2, and though I think it is reasonable to request users to update Ant to the latest version available, I have no control over what OS and libraries they have installed. As such, I want to solve the problem not by yum install something or whatnot, but by finding a way to properly use the JUnit jar files(if that is possible). Then, when they pull the git repository and run the task, it will work on any OS or environment(since they will be using a jar file they just pulled). Obviously, this also means that "just put the jars in the Ant lib dir" will not be a viable solution.
Thank you.

StubbornShowaGuy
  • 249
  • 4
  • 18
  • Have you considered using Maven to download the necessary jar? If not this could help you: http://stackoverflow.com/questions/25628253/ant-junit-task-where-to-download-ant-junit-jar-and-where-to-put-it – Gildraths May 24 '16 at 09:25

2 Answers2

2

To fix this error you have to pass to ant the jar that is defining the JUnitTask. This jar is called ant-junit.jar and you can either (from JUnit task documentation) :

  1. Put both junit.jar and ant-junit.jar in ANT_HOME/lib.
  2. Do not put either in ANT_HOME/lib, and instead include their locations in your CLASSPATH environment variable.
  3. Add both JARs to your classpath using -lib.
  4. Specify the locations of both JARs using a <classpath> element in a <taskdef> in the build file.
  5. Leave ant-junit.jar in its default location in ANT_HOME/lib but include junit.jar in the passed to . (since Ant 1.7)

For option 4, you just have to add to your ant build a declaration like:

<taskdef name="junit" classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask">
  <classpath>
    <pathelement location="${lib}/ant-junit.jar"/>
    <pathelement location="${lib}/ant-junit4.jar"/>
  </classpath>
</taskdef>
P.A. Cros
  • 501
  • 3
  • 6
  • Maybe I misunderstood you, but your solution seems to advance the problem in the right direction, but not solve it: The result I got was, if I use the ant-junit for JUnit 4: `taskdef class org.apache.tools.ant.taskdefs.optional.junit.JUnitTask cannot be found` and for the pre-4 one, the tests actually run but give the error `junit.framework.AssertionFailedError: No tests found`. My tests are written for JUnit 4(No `extends TestCase`, using `@Test` annotations) and I am using the JUnit 4 library, so if you have any more advice on how to make it work for JUnit 4, it would be greatly appreciated. – StubbornShowaGuy May 25 '16 at 02:03
  • @StubbornShowaGuy you are right: for JUnit 4 tests, you also need to add `ant-junit4.jar` to the classpath of the taskdef (see my edited answer). I also remove `junit-4.12.jar` and `hamcrest-core-1.3.jar` from that classpath, since they are not needed for taskdef declaration and you already add them to the `junit` task classpath in your build. Hope that it solve your problem – P.A. Cros May 25 '16 at 07:30
  • I just did that, and now it works perfectly. Thank you! – StubbornShowaGuy May 25 '16 at 09:17
0

This also work perfectly for me too after I copied the jar files over to my test Linux box. Since the layout was not shown before, this is mine.

ls -l /usr/share/ant/lib
total 0
lrwxrwxrwx. 1 root root 32 Mar 24  2021 ant-bootstrap.jar -> ../../java/ant/ant-bootstrap.jar
lrwxrwxrwx. 1 root root 28 Oct 12 19:10 ant-junit.jar -> ../../java/ant/ant-junit.jar
lrwxrwxrwx. 1 root root 29 Oct 12 19:11 ant-junit4.jar -> ../../java/ant/ant-junit4.jar
lrwxrwxrwx. 1 root root 31 Mar 24  2021 ant-launcher.jar -> ../../java/ant/ant-launcher.jar
lrwxrwxrwx. 1 root root 22 Mar 24  2021 ant.jar -> ../../java/ant/ant.jar

ls /usr/share/java/ant
ant-bootstrap.jar  ant-junit.jar  ant-junit4.jar  ant-launcher.jar  ant.jar
John K
  • 1
  • 2