Eclipse环境中Junit结合Ant进行自动测试的问题(2)

Posted by dohkoos on February 23rd, 2005 (750 views)

Environment:eclipse-3.0.3 + ant-1.6.2 + junit-3.8.1

// build.xml
<target name="test">
<junit>
    <formatter type="xml" />
    <batchtest todir="${report.dir}">
        <fileset dir="${classes.dir}" includes="**/*Test.class" />
    </batchtest>
</junit>
</target>

执行脚本的时候出现了以下的错误:

java.lang.ClassNotFoundException: example.AdditionTest
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)

将<fileset dir="${classes.dir}" includes="**/*Test.class" /> 改成<...includes="**/*Test.*" />
或者<...includes="**/*Test" /> 都不行。只有改成<...includes="**/*Test.java" />才可以,可
是改成<...includes="**/*Test.java" /> 后测试无任何效果,肯定通过。用google 搜也搜
不到答案,到ant 网站http://ant.apache.org/faq.html#delegating-classloader上去查找,才
找到原因。

在上述情况中,Ant 不会去加载example.AdditionTest。而只是加载了junit库,junit
再去加载example.AdditionTest,由于java 加载采用的是delegation 模式,而且junit 和
example.AdditionTest 不在同一个空间,所以junit 会调用parent 的classLoader 来加载
库,结果就出现上面的ClassNotFoundException 提示信息啦。

因此,只要载<junit></junit> 间加入classpath 指出要加载库的路径就可以了,修改
后的build.xml 文件如下:
// build.xml
<target name="test">
<junit>
    <classpath>
            <pathelement location="${classes.dir}" />
    </classpath>
    <formatter type="xml" />
    <batchtest todir="${report.dir}">
        <fileset dir="${classes.dir}" includes="**/*Test.class" />
    </batchtest>
</junit>
</target>

Related Posts

People who read this, also read...

Eclipse环境中Junit结合Ant进行自动测试的问题(1)

Posted by dohkoos on February 6th, 2005 (702 views)

Eclipse 3.0.3
Ant 1.6.2
Junit 3.8.1

build.xml

<target name="test">
    <junit>
        ...
    </junit>
</target>

在执行时出现了以下问题:

BUILD FAILED: C:eclipse-3.0.3workspaceDailyBuildbuild.xml:44: Could not create task or type of type: junit.

Ant could not find the task or a class this task relies upon.

This is common and has a number of causes; the usual
solutions are to read the manual pages then download and
install needed JAR files, or fix the build file:
 - You have misspelt 'junit'.
   Fix: check your spelling.
 - The task needs an external JAR file to execute
     and this is not found at the right place in the classpath.
   Fix: check the documentation for dependencies.
   Fix: declare the task.
 - The task is an Ant optional task and the JAR file and/or libraries
     implementing the functionality were not found at the time you
     yourself built your installation of Ant from the Ant sources.
   Fix: Look in the ANT_HOME/lib for the 'ant-' JAR corresponding to the
     task and make sure it contains more than merely a META-INF/MANIFEST.MF.
     If all it contains is the manifest, then rebuild Ant with the needed
     libraries present in ${ant.home}/lib/optional/ , or alternatively,
     download a pre-built release version from apache.org
 - The build file was written for a later version of Ant
   Fix: upgrade to at least the latest release version of Ant
 - The task is not an Ant core or optional task
     and needs to be declared using <taskdef>.
 - You are attempting to use a task defined using
    <presetdef> or <macrodef> but have spelt wrong or not
   defined it at the point of use

Remember that for JAR files to be visible to Ant tasks implemented
in ANT_HOME/lib, the files must be in the same directory or on the
classpath

Please neither file bug reports on this problem, nor email the
Ant mailing lists, until all of these causes have been explored,
as this is not an Ant bug.

产生上面的错误原因是Ant在执行Junit程序时没有找到junit.jar这个文件,将junit.jar文件copy到ANT_HOME/lib目录下,再次执行ant test任务,waiting...

这下该OK了吧^_^

有没有搞错,还产生同样的错误!到窗口下试试。进入命令行窗口,输入ant test,再次等待ing

Buildfile: build.xml

test:

BUILD SUCCESSFUL
Total time: 1 second

怎么会这样的???命令行窗口下可以,Eclipse下就不行了。

到Preferences -> Ant -> Runtime -> Classpath下设置一下junit.jar的路径,再次执行ant test任务,终于在console中看到了

test:

BUILD SUCCESSFUL
Total time: 4 seconds

Related Posts

People who read this, also read...