java - JUnit AntClassNotFoundException

标签 java ant junit

我在运行“ant test”目标时收到 ClassNotFoundException。 所有路径和库都存在,包括位于 lib 文件夹中的 junit-4.8.1.jar。

<project name="progs" default="test" basedir=".">

  <!--++++++++++ Properties ++++++++++-->
  <property name="src-main" location="PC_Serie_3"/>
  <property name="src-test" location="PC_Serie_3_Tests"/>
  <property name="target"  location="target"/>
  <property name="target-classes"  location="target/PC_Serie_3"/>
  <property name="target-test-classes"  location="target/PC_Serie_3_Tests"/>
  <property name="target-test-reports"  location="target/test-reports"/>

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

  <!--++++++++++ Targets ++++++++++-->
  <target name="init" description ="Creates the target folders">
    <mkdir dir="${target-classes}"/>
    <mkdir dir="${target-test-classes}"/>
    <mkdir dir="${target-test-reports}"/>
  </target>

  <target name="clean" description="Removes the target folders" >
    <delete includeEmptyDirs="true" failonerror="false" verbose="true" >
        <fileset dir="${target}" defaultexcludes="false"/>
    </delete>
  </target>

  <target name="compile-main" depends="init" description="Compiles the main source" >
    <javac debug="true"
           srcdir="${src-main}"
           destdir="${target-classes}"
           includeantruntime="false">
    </javac>
  </target>

  <target name="compile-test" depends="compile-main" description="Compiles the test source" >
    <javac  debug="true"
            debugLevel="source"
            srcdir="${src-test}"
            destdir="${target-test-classes}"
            includeantruntime="true">
      <classpath>
        <pathelement location="${target-classes}"/>
        <path refid="test.extlibs.class.path" />
      </classpath>
    </javac>
  </target>

<target name="test" depends="compile-test" description="Runs the tests">
    <echo>Running the junit tests...</echo>
    <junit printsummary="yes" haltonfailure="true" showoutput="true" >
      <classpath>
        <pathelement location="${src-main}"/>
        <pathelement location="${target-classes}"/>
        <pathelement location="${target-test-classes}"/>
        <path refid="test.extlibs.class.path" />
      </classpath>

    <batchtest fork="yes" todir="${target-test-reports}" >
        <fileset dir="${src-test}">
          <include name="**/*Test*.java"/>
        </fileset>
        <formatter type="xml"/>
        <formatter type="plain" usefile="false" />
      </batchtest>
      </junit>
  </target>

  <target name="package" depends="test" description="Packages the main classes into a jar" >
    <buildnumber />
    <jar jarfile="${target}/library.jar" basedir="${target-classes}"/>
  </target>

    <!-- USAGE: ant deploy-app -->
    <target name="deploy-lib" depends="package">
    </target>
</project>

控制台输出:

测试:

    [echo] Running the junit tests...
    [junit] Running src.DocumentDBTests.DocumentDBv1Test
    [junit] Testsuite: src.DocumentDBTests.DocumentDBv1Test
    [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]     Caused an ERROR
    [junit] src.DocumentDBTests.DocumentDBv1Test
    [junit] java.lang.ClassNotFoundException: src.DocumentDBTests.DocumentDBv1Te
st
    [junit]     at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    [junit]     at java.security.AccessController.doPrivileged(Native Method)
    [junit]     at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    [junit]     at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    [junit]     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)

    [junit]     at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    [junit]     at java.lang.Class.forName0(Native Method)
    [junit]     at java.lang.Class.forName(Class.java:169)
    [junit]

最佳答案

我认为问题在于您将 junit 指向源文件而不是已编译的类文件。此外,文件集 包含 *Test*.java 而它应该包含 *Test*.class

尝试用这个替换你的 batchtest 元素:

<batchtest fork="yes" todir="${target-test-reports}" >
    <fileset dir="${target-test-classes}">
        <include name="**/*Test*.class"/>
    </fileset>
    <formatter type="xml"/>
    <formatter type="plain" usefile="false" />
</batchtest>

关于java - JUnit AntClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4418343/

相关文章:

java - 当服务部署在服务器中时,如何进行 JUnit 测试 Jersey?

java - 如何删除edittext字段的边框

java.lang.NoClassDefFoundError : sun/net/www/protocol/https/DefaultHostnameVerifier

java - 避免多个对象事务死锁的最佳方法?

java - Ant 构建 - Emma 代码检测 - JUnit 测试需要接口(interface),Emma 不检测它们

ant - SonarQube 代码覆盖率 - 排除某些类

java - 用于维护 Java 中的 Object 方法契约的 Automagic 单元测试?

java - 使用 h2 数据库的 JUnit 测试表现异常

java - Hibernate继承、Collections和@OrderedBy父类(super class)属性产生MySQL语法错误

java - Ant build.xml需要用户输入,但Eclipse没有tty