JUnit with Ant,另一个java.lang.ClassNotFoundException

时间:2016-04-30 04:14:48

标签: java junit ant classnotfoundexception

Ant的新手,真的不知道我在做什么。我的猜测是我没有正确设置JUnit类路径。我看到有很多类似的问题,虽然没有一个建议对我有用。请帮忙!

我的项目目录结构:(我怀疑我的目录结构严重混乱)

MyProject/
    bin/main/
        project.class
        test1.class
        test2.class
        test3.class
    build/main/project
        Project.class
    build/test/main
        test1.class
        test2.class
        test3.class
    build/test/project
        test1.class
        test2.class
        test3.class
    lib/
        junit-4.11.jar
        hamcrest-all-1.3.jar
    src/main/java/
        project.java
    src/test/java/
        test1.java
        test2.java
        test3.java
    build.xml

我的build.xml

<project name="junit-example">
  <property name="main.build.dir" value="build/main"/>
  <property name="main.src.dir" value="src/main/java"/>
  <property name="test.build.dir" value="build/test"/>
  <property name="test.src.dir" value="src/test/java"/>

  <path id="classpath.test">
    <pathelement location="lib/junit-4.11.jar"/>
    <pathelement location="lib/hamcrest-core-1.3.jar"/>
    <pathelement location="${main.build.dir}"/>
  </path>

  <target name="compile">
    <mkdir dir="${main.build.dir}"/>
    <javac srcdir="${main.src.dir}" destdir="${main.build.dir}" includeantruntime="false"/>
  </target>

  <target name="test-compile" depends="compile">
    <mkdir dir="${test.build.dir}"/>
    <javac srcdir="${test.src.dir}" destdir="${test.build.dir}" includeantruntime="false">
        <classpath refid="classpath.test"/>
    </javac>
  </target>

  <target name="testAll" depends="test-compile">
    <junit printsummary="on" haltonfailure="yes" fork="no">
        <classpath>
          <path refid="classpath.test"/>
          <pathelement location="${test.build.dir}"/>
        </classpath>
        <formatter type="brief" usefile="false" />
        <batchtest>
            <fileset dir="${test.src.dir}" includes="**/*Test.java" />
        </batchtest>
    </junit>
  </target>
</project>

1 个答案:

答案 0 :(得分:2)

您将batchtest定义为

<batchtest>
    <fileset dir="${test.src.dir}" includes="**/*Test.java" />
</batchtest>

然而,${test.src.dir}中的文件是

src/test/java/
    test1.java
    test2.java
    test3.java

尝试将batchtest更改为

<batchtest>
    <fileset dir="${test.src.dir}" includes="**/*test?.java" />
</batchtest>