通过Ant执行的JUnit保持失败

时间:2013-02-10 21:16:13

标签: ant junit

执行简单的junit测试类时,我不断收到java.lang.ClassNotFoundException。 我只是找不到有什么问题.. 这是我的文件系统。主应用程序编译正常。 这是我的蚂蚁文件:

<project name="MyProject" default="dist" basedir=".">
    <description>
       controller build
    </description>
  <!-- set global properties for this build -->
  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist"  location="dist"/>
  <property name="TALK" value="true" />


  <path id="class.path">
      <fileset dir="..\lib">
        <include name="**/*.jar" />
      </fileset>      
  </path>


  <path id="test.classpath">
    <pathelement location="${build}" />
    <pathelement location="..\lib\junit\junit.jar" />
      <fileset dir="..\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}"/>
  </target>

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

  <target name="dist" depends="compile,test"
        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/controller.jar" basedir="${build}"/>
  </target>

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

  <target name="test" depends="compile">
    <junit printsummary="yes" showoutput="true">
      <classpath refid="test.classpath"/>      
      <formatter type="brief" usefile="false" />
      <test name="workflowShouldHaveValidXML" />
      <!--test name="environmentHasValidXml" /-->
      <!--test name="workflowHasValidXml" /-->
    </junit>
  </target>


</project>

这是我得到的错误:

D:\svn\trunk\controller>ant  -f build_controller.xml
Buildfile: D:\svn\trunk\controller\build_controller.xml

init:

compile:
    [javac] D:\svn\trunk\controller\build_controller.xml:37: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false
 for repeatable builds

test:
    [junit] Running workflowShouldHaveValidXML
    [junit] Testsuite: workflowShouldHaveValidXML
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
    [junit]
    [junit] Null Test:  Caused an ERROR
    [junit] workflowShouldHaveValidXML
    [junit] java.lang.ClassNotFoundException: workflowShouldHaveValidXML
    [junit]     at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    [junit]     at java.lang.Class.forName0(Native Method)
    [junit]     at java.lang.Class.forName(Class.java:264)
    [junit]
    [junit]
    [junit] Test workflowShouldHaveValidXML FAILED

dist:

BUILD SUCCESSFUL
Total time: 0 seconds

这是我的目标结构:

svn\trunk\controller\build_controller.xml    \\ Ant file
svn\trunk\controller\src\com\controller\core\*.java  \\ main app sources
svn\trunk\controller\dist\lib\controller.jar  \\ the main product 
svn\trunk\controller\build\com\controller\core\*.class   \\the main product *.class
svn\trunk\controller\test\com\controller\core\workflowShouldHaveValidXML.java  \\ the test case class
svn\trunk\lib\junit\junit.jar 
svn\trunk\lib\java\*.jar   \\ third party jars

1 个答案:

答案 0 :(得分:0)

您需要使用完全限定的类名(包括包),而不仅仅是类的简单名称:

在你的junit任务中,试试:

<test name="com.controller.core.workflowShouldHaveValidXML" />