【问题标题】:Junit test integrated with Ant Failed with ClassNotFoundException与 Ant 集成的 Junit 测试因 ClassNotFoundException 而失败
【发布时间】:2011-10-22 12:07:26
【问题描述】:

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

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

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

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

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 指向的类路径中很好。有人会有解决这个问题的想法吗?

感谢您的帮助。

西尔万。

【问题讨论】:

    标签: ant junit classnotfoundexception


    【解决方案1】:

    ${testclasses.home} 目录不在 &lt;junit&gt; 任务的类路径中。

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

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

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

    【讨论】:

    • 感谢您的回答。它解决了我的问题。我忘记将测试类放在我的 junit ant 任务的类路径中。
    • 这个解决方案在我的例子中运行到另一个错误:java.util.zip.ZipException;看这里:stackoverflow.com/questions/8655193/… 在 Mayoares 回答。将上面的标签 更改为 帮助我纠正了这个错误。
    猜你喜欢
    • 2012-05-27
    • 2019-05-01
    • 2011-07-23
    • 1970-01-01
    • 2017-01-25
    • 2020-03-18
    • 2012-11-04
    • 2013-05-22
    • 1970-01-01
    相关资源
    最近更新 更多