简体   繁体   English

带有Ant的JUnit,另一个java.lang.ClassNotFoundException

[英]JUnit with Ant, another java.lang.ClassNotFoundException

New to Ant, don't really know what I'm doing. 第一次接触Ant,真的不知道我在做什么。 My guess is that I did not set the JUnit classpath correctly. 我的猜测是我没有正确设置JUnit类路径。 I see there are a lot of similar questions as this one, Although none of the suggestions have worked for me. 我看到有很多与此问题类似的问题,尽管没有任何建议对我有用。 Please help! 请帮忙!

My project directory structure: (I suspect my dir structure is seriously messed up) 我的项目目录结构:(我怀疑我的目录结构被严重搞乱了)

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

My 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>

You defined batchtest as 您将batchtest定义为

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

Yet your the files in ${test.src.dir} are 但是,您在${test.src.dir}中的文件是

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

Try changing batchtest to 尝试将batchtest更改为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM