与 Ant 集成的 Junit 测试因 ClassNotFoundException 失败 [英] Junit test integrated with Ant Failed with ClassNotFoundException

查看:30
本文介绍了与 Ant 集成的 Junit 测试因 ClassNotFoundException 失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目有 JUnit 测试,可以在 Eclipse 中正确运行.

I have JUnit tests on my project that run correctly with Eclipse.

所以,现在我尝试将这些测试与 ant 任务集成.为此,我制作了以下蚂蚁脚本:

So, now I try to integrate these tests with an ant task. To make that I make the following ant script :

<path id="classpath-test">
    <pathelement path="." />
    <pathelement path="${classes.home}" />
    <fileset dir="${lib.home}" includes="*.jar" />
    <fileset dir="${libtest.home}" includes="*.jar" />
</path>

    <target name="compile" ... > // compiles src code of the project

<target name="compile-tests" depends="compile">
    <javac  srcdir="${test.home}"
            destdir="${testclasses.home}" 
            target="1.5"
            source="1.5" 
            debug="true"
        >
        <classpath refid="classpath-test" />
    </javac>

    <copy todir="${testclasses.home}">
        <fileset dir="${test.home}">
            <exclude name="**/*.java"/>
        </fileset>
    </copy>
</target>

<target name="unit-test" depends="compile-tests">
    <junit printsummary="false" fork="off" haltonfailure="true">
        <classpath refid="classpath-test" />

        <formatter type="brief" usefile="false" />

        <test name="com.test.MyTest" />

        <!--<batchtest todir="${reports.dir}" >
            <fileset dir="${testclasses.home}" >
                <exclude name="**/AllTests*"/>
                <include name="**/*Test.class" />
            </fileset>
        </batchtest>-->
    </junit>
</target>

${libtest.hom} 目录包含 junit-4.8.1.jar 和 hamcrest-core-1.1.jar.

The directory ${libtest.hom} contains junit-4.8.1.jar and hamcrest-core-1.1.jar.

当我启动以下命令:ant unit-test 时,MyTest 的执行失败,输出如下:

When I launch the following command : ant unit-test, the execution of the MyTest fails with the following output :

unit-test:
[junit] Testsuite: com.test.MyTest
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
[junit]
[junit] Null Test:  Caused an ERROR
[junit] com.test.MyTest
[junit] java.lang.ClassNotFoundException: com.test.MyTest
[junit]     at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
[junit]     at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:316)
[junit]     at java.lang.Class.forName0(Native Method)
[junit]     at java.lang.Class.forName(Class.java:247)
[junit]
[junit]

这很奇怪,因为 com.test.MyTest 在 ant 脚本中为我的任务 junit 指向的类路径中很好.有人会有解决这个问题的想法吗?

It's very strange because com.test.MyTest is well in the classpath pointed for my task junit in ant script. Someone would have and idea to solve this problem ?

感谢您的帮助.

西尔万.

推荐答案

${testclasses.home} 目录不在 任务的类路径上.

The ${testclasses.home} directory is nowhere on a classpath for <junit> task.

我认为这是 com.test.MyTest 的类文件所在.

I think this is where class file for com.test.MyTest lives.

这是修改后的单元测试目标:

Here is modified unit-test target:

<target name="unit-test" depends="compile-tests">
    <junit printsummary="false" fork="off" haltonfailure="true">
        <classpath>
          <path refid="classpath-test"/>
          <fileset dir="${testclasses.home}"/>
        </classpath>

        <formatter type="brief" usefile="false" />

        <test name="com.test.MyTest" />

        <!--<batchtest todir="${reports.dir}" >
            <fileset dir="${testclasses.home}" >
                <exclude name="**/AllTests*"/>
                <include name="**/*Test.class" />
            </fileset>
        </batchtest>-->
    </junit>
</target>

这篇关于与 Ant 集成的 Junit 测试因 ClassNotFoundException 失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆