java - Ant + Junit 问题

标签 java ant junit

我正在尝试让 Junit 与 Ant 一起工作。我遇到过有关该主题的问题。我想某处有一些小错误,但我无法弄清楚。这是我的构建文件:

        <?xml version="1.0" encoding="UTF-8"?>
    <project name="IvleFileSync" default="dist" basedir=".">
    <description>
        simple example build file
    </description>
      <!-- set global properties for this build -->
      <property name="src" location="src"/>
      <property name="build" location="build"/>
      <property name="dist"  location="dist"/>
        <!-- Variables used for JUnit testing -->
        <property name="test.dir" location="test" />
        <property name="test.report.dir" location="test-reports" />
     <path id="junit-classpath">
    <fileset dir="${test.dir}">
        <include name = "*" />
    </fileset>
     </path>

    <path id="files-classpath">
    <fileset dir="/usr/lib" >
        <include name="*.jar"/>
    </fileset>
    </path> 

      <target name="init">
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>
    <mkdir dir="${test.report.dir}" />
      </target>

      <target name="compile" depends="init" description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
     <javac srcdir="${src}" destdir="${build}"> 
           <classpath>
        <path refid="files-classpath" />
    </classpath> 
    </javac>

       </target>

      <target name="dist" depends="compile"
        description="generate the distribution" >
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}/lib"/>

    <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
    <jar jarfile="${dist}/lib/IvleFileSync-${DSTAMP}.jar" basedir="${build}"/>
      </target>

    <target name="compile-test" depends="compile" description="compile the tests " >
       <!-- Compile the java code from ${src} into ${build} -->
       <javac srcdir="${test.dir}" destdir="${build}"> 
         <classpath>
          <path refid="files-classpath" />
       </classpath> 
       </javac>

    </target>

    <target name="test" depends="compile-test" description="Execute Unit Tests" >
      <junit printsummary="yes" fork="yes" haltonfailure="yes" showoutput="false">
       <classpath >
    <path refid="files-classpath" />
    <path refid= "junit-classpath" />
       </classpath>
    <batchtest fork="yes" todir="${test.report.dir}/"> 
            <formatter type="xml"/> 
            <fileset dir="${test.dir}"> 
                <include name="*Test*.java"/> 
            </fileset> 
    </batchtest> 
      </junit>
    </target>

      <target name="clean"
        description="clean up" >
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${test.report.dir}" />
    <delete dir="${dist}"/>
      </target>
    </project>

我在/test 目录中有测试文件,并且我已将 jar 放在 ANT_HOME/lib

这不起作用,当我挖掘 test-results/....xml 时出现此错误

<error message="NewEmptyJUnitTest"     type="java.lang.ClassNotFoundException">java.lang.ClassNotFoundException: NewEmptyJUnitTest
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)

谢谢你帮我...

最佳答案

junit 任务的类路径不包括编译测试目标的输出。对 JUnit 类路径使用类似以下内容:

<classpath>
  <pathelement path="${build}"/>
  <path refid="files-classpath" />
  <path refid="junit-classpath" />
</classpath>

关于java - Ant + Junit 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8963724/

相关文章:

java - 如何使用 Html.fromHtml 在 android 中显示心脏标志

Java程序计算

Java双端队列toString方法覆盖左侧元素

java - ant 中的类路径验证

java - 如何使用 CriteriaQuery 查询 OneToOne 实体的特定值

git - 在构建过程中获取git分支名称

android - Ant,错误膨胀类android.support.v7.widget.RecyclerView

java - gradle:如何从根项目运行子项目的 JUnit 测试?

unit-testing - 适用于所有测试的 JUnit 理论 - 仅需要参数化设置方法

eclipse - 如何通过 eclipse 在 JUnit 测试中使用 persistence.xml 和 hibernate.cfg.xml?