How to solve “Cause: the class org.apache.tools.ant.taskdefs.optional.junit.JUnitTask was not found.” while running “ant test”?

前端 未结 5 1557
慢半拍i
慢半拍i 2021-01-01 13:31

I have a target named test and I want to do some tests.

I put here the important parts in build.xml. It includes:



        
相关标签:
5条回答
  • 2021-01-01 14:07

    In my case (using Mint, based on Debian)

    sudo apt-get install ant-optional
    

    was the only thing that worked.

    0 讨论(0)
  • 2021-01-01 14:07

    The issue was solved on RHEL-based systems once ant-junit was installed:

    $ sudo yum install ant-junit
    
    0 讨论(0)
  • 2021-01-01 14:11

    When you run JUnit tasks, you must make sure both your classpath that was used for your builds, and the path for the junit jars are added to your classpath.

    I prefer to put the junit jars inside my project, so others don't have to install them in their machines for my build to work. I would install them in ${basedir}/antlib/junit

    I use ${basedir}/antlib to store all the various Ant build related jars such as the Ant-Contrib jars, JaCoCo, Findbugs, etc. Since this is inside my project, a checkout will always include these jars and the build will work for everyone:

     <classpath id="junit.path">
        <fileset dir="${basedir}/antlib/junit"/>
    </classpath>
    

    This will create a path that contains your JUnit jars. Now to compile your junit tests:

    <javac dest="${target.dir}/test-classes"
       src=${src.test.java}">
       <classpath refid="javac.classpath"/>   <!-- Your main build classpath -->
       <classpath refid="junit.path"/>        <!-- Your JUnit classpath -->
       <classpath path="${main.destdir}"/>    <!-- These the classes you've built-->
    

    Note that placing the JUnit jars in $ANT_HOME/lib main not work because it depends whether <javac> has includeAntRuntime set or not. It's always highly recommended not to have this set in order to make your build more platform independent.

    Also remember to include that junit.path in your <junit> task too.

    0 讨论(0)
  • 2021-01-01 14:16

    I was seeing this message because I had missed to include the ant-junit.jar from my IDE's Classpath, e.g. in Eclipse > Right click on your project > Run as > Run Configurations.. > Classpath (tab) > Ensure the ant-junit.jar is there.

    0 讨论(0)
  • 2021-01-01 14:24
    <property name='lib.dir' value='lib' />
    
    <path id='classpath'>
        <fileset dir="${lib.dir}" includes="**/*.jar" />
    </path>
    

    this has nothing to do with Ant classpath itself. It is properties you can use in your tasks. You have to put jars to the suggested dirs or add command line argument.

    Try running it like this:

    ant -lib /path/to/the/ant-junit.jar/ test
    
    0 讨论(0)
提交回复
热议问题