JUnit 蚂蚁 ClassNotFoundException

JUnit Ant ClassNotFoundException

当我 运行 我的目标 TranslatorWorkflow 应该执行 JUnit 测试时,我得到一个 java.lang.ClassNotFoundException。我正在 运行 创建一个 build.xml 文件,目标是:build TranslatorWorkflow。它编译但立即在 JUnit 测试中失败。 我的 TranslatorWorkflow.class 文件在 {basedir}/bin/testScripts/ 中。我的类路径和目标是:

类路径:

<path id="classpath">
    <fileset dir="${basedir}/lib/binaries" includes="*.jar" />
    <pathelement location="${basedir}/bin/testScripts/" />
</path>

我的 build.xml 文件中的 TranslatorWorkflow 目标:

<target name="TranslatorWorkflow">
    <mkdir dir="${junit.output.dir}" />
    <junit fork="yes" printsummary="withOutAndErr">
        <formatter type="xml" />
        <test name="testScripts.TranslatorWorkflow" todir="${junit.output.dir}" />
        <classpath refid="classpath" />
    </junit>
</target>

我试图模仿 this answer to a similar question by adding the pathelement line shown in my classpath section above, but received the same exception. I've looked at this question as well as it seems like the same deal。我想我错过了一些非常明显的东西,但可惜我似乎没有得到它。

Dumpcats,试试这个...

  <path id="base.path">
    <pathelement path="${sun.boot.class.path}"/>
    <pathelement path="${sun.boot.library.path}"/>
    <fileset dir="${basedir}"> 
       <include name="**.jar"/>
    </fileset>
  </path>

然后在目标元素中...

<target name="runTest">
  <mkdir dir="test_reports"/>

  <junit
      fork="true"
      forkmode="once"
      maxmemory="256m">

    <formatter type="plain" usefile="false"/>
    <formatter type="xml"/>

    <classpath refid="base.path"/>

    <batchtest
        haltonerror="true" haltonfailure="true"
        todir="test_reports">

      <fileset dir="${test.build}">
        <include name="**/*Test*.class"/>
        <include name="**/Test*.class"/>
        <include name="**/*Test.classp"/>
        <!-- Exclusions -->
        <exclude name="**/util/*.class"/> 
      </fileset>
    </batchtest>
  </junit>
</target>

至少我是这样管理类路径引用的,而且一切都很顺利。

class路径应该引用 ${basedir}/bin 而不是 ${basedir}/bin/testScripts(即它应该引用 classes 目录的根目录,而不是 class存在):

<path id="classpath">
   <fileset dir="${basedir}/lib/binaries" includes="*.jar" />
   <pathelement location="${basedir}/bin/" />
</path>