【问题标题】:ClassNotFoundException - when running ant-junitClassNotFoundException - 运行 ant-junit 时
【发布时间】:2014-09-09 21:42:33
【问题描述】:

我正在尝试为实际应用程序创建概念证明,任务是运行 ant-junit 测试而不向全局环境添加东西。到目前为止,我已经在 ant 中实现了它,但是,我遇到了以下异常。 ClassTest.java 是具有示例单元测试用例的 java 类。 (Junit 3.0 风格)。我不确定为什么 ant-junit 没有找到该类,尽管在批处理任务中提到了路径。

项目结构

运行 ant-junit 任务时出现以下异常。

Testsuite: ClassTest
Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec

    Caused an ERROR
ClassTest
java.lang.ClassNotFoundException: ClassTest
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:249)
    at org.eclipse.ant.internal.launching.remote.EclipseSingleCheckExecutor.executeTargets(EclipseSingleCheckExecutor.java:30)
    at org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
    at org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:424)
    at org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:138)

我的 ant build.xml 文件是

<project name="Java project build" default="build">
<property name="project.local.directory" value="C:\Users\usrpao\workspace\Ant" />
<property name="src.path" value="${project.local.directory}/src" />
<property name="lib.path" value="${project.local.directory}/lib" />
<property name="dest.path" value="${project.local.directory}/target" />
<property name="junit.output.dir" value="${project.local.directory}/junit" />

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

<!--<taskdef name="junit" classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask">
    <classpath refid="classpath.test" />
</taskdef> -->

<path id="MyProject.classpath">
    <pathelement location="lib/ant-junit.jar" />
    <pathelement location="lib/junit.jar" />
        <pathelement location="bin/*.*"/>
</path>

<path id="lib.classpath">
    <pathelement location="lib/ant-junit.jar" />
    <pathelement location="lib/junit.jar" />
</path>

<target name="build" depends="clean">
    <javac srcdir="${src.path}" destdir="${dest.path}" classpathref="lib.classpath">
    </javac>
    <antcall target="test">
    </antcall>
</target>

<target name="clean">

</target>

<target name="test">
    <pathconvert property="testoutput" refid="classpath.test" />
    <echo>Path = ${testoutput}</echo>
    <mkdir dir="${junit.output.dir}" />
    <junit haltonerror="false" showoutput="true">

        <classpath refid="MyProject.classpath">
        </classpath>
        <batchtest todir="${junit.output.dir}">
            <formatter type="plain" />
            <fileset dir="${src.path}/com/ant/test">
                <include name="**/*Test*.java" />
            </fileset>
        </batchtest>

    </junit>
</target>

【问题讨论】:

  • 为什么在MyProject.classpath 中包含&lt;pathelement location="bin/*.*"/&gt;?不应该是&lt;pathelement location="${dest.path}"/&gt;吗?
  • @manouti 我会试试的
  • @manouti 不起作用。

标签: java eclipse ant ant-junit


【解决方案1】:

您的类路径引用bin 目录,而类实际上位于target 目录下。您应该将MyProject.classpath 引用更改为包含${project.local.directory}/target

<path id="MyProject.classpath">
   <pathelement location="lib/ant-junit.jar" />
   <pathelement location="lib/junit.jar" />
   <dirset dir="${project.local.directory}">
        <include name="target"/>
    </dirset>
</path>

另外,你应该改变junit任务中的fileset元素,移除测试类包名对应的文件夹,否则你仍然会得到一个ClassNotFoundException

<junit haltonerror="false" showoutput="true">

    <classpath refid="MyProject.classpath">
    </classpath>
    <batchtest todir="${junit.output.dir}">
        <formatter type="plain" />
        <fileset dir="${src.path}">  <!-- remove com/ant/test corresponding to package name -->
            <include name="**/*Test*.java" />
        </fileset>
    </batchtest>

</junit>

【讨论】:

【解决方案2】:

从您的文件夹结构中可以明显看出ClassTest.class 类位于target 文件夹中。所以在class-path 你应该包括以下内容:

<pathelement path="target"/>

希望这会有所帮助。

【讨论】:

  • 那没用,我把文件夹放在 路径类路径元素,不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-02
  • 1970-01-01
相关资源
最近更新 更多