首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JUnit AntClassNotFoundException

JUnit AntClassNotFoundException
EN

Stack Overflow用户
提问于 2010-12-12 02:50:29
回答 1查看 846关注 0票数 2

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

代码语言:javascript
复制
<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>

控制台输出:

测试:

代码语言:javascript
复制
    [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]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-12-13 02:55:40

我认为问题在于您将junit指向源文件,而不是编译后的类文件。此外,fileset在本应包含*Test*.java的情况下包含了*Test*.class

尝试将batchtest元素替换为以下内容:

代码语言:javascript
复制
<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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4418343

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档