配置ant以运行单元测试.图书馆应该在哪里?应该如何配置classpath?避免ZipException

chi*_*tom 19 java ant junit

我正在尝试使用ant运行我的junit测试.测试是使用JUnit 4测试套件启动的.如果我直接从Eclipse运行它,测试完成没有错误.但是,如果我从ant运行它,那么许多测试都会失败,并且一遍又一遍地重复此错误,直到junit任务崩溃.

    [junit] java.util.zip.ZipException: error in opening zip file
    [junit]     at java.util.zip.ZipFile.open(Native Method)
    [junit]     at java.util.zip.ZipFile.(ZipFile.java:114)
    [junit]     at java.util.zip.ZipFile.(ZipFile.java:131)
    [junit]     at org.apache.tools.ant.AntClassLoader.getResourceURL(AntClassLoader.java:1028)
    [junit]     at org.apache.tools.ant.AntClassLoader$ResourceEnumeration.findNextResource(AntClassLoader.java:147)
    [junit]     at org.apache.tools.ant.AntClassLoader$ResourceEnumeration.nextElement(AntClassLoader.java:130)
    [junit]     at org.apache.tools.ant.util.CollectionUtils$CompoundEnumeration.nextElement(CollectionUtils.java:198)
    [junit]     at sun.misc.CompoundEnumeration.nextElement(CompoundEnumeration.java:43)
    [junit]     at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.checkForkedPath(JUnitTask.java:1128)
    [junit]     at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeAsForked(JUnitTask.java:1013)
    [junit]     at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:834)
    [junit]     at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeOrQueue(JUnitTask.java:1785)
    [junit]     at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:785)
    [junit]     at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
    [junit]     at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    [junit]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    [junit]     at java.lang.reflect.Method.invoke(Method.java:597)
    [junit]     at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    [junit]     at org.apache.tools.ant.Task.perform(Task.java:348)
    [junit]     at org.apache.tools.ant.Target.execute(Target.java:357)
    [junit]     at org.apache.tools.ant.Target.performTasks(Target.java:385)
    [junit]     at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1337)
    [junit]     at org.apache.tools.ant.Project.executeTarget(Project.java:1306)
    [junit]     at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
    [junit]     at org.apache.tools.ant.Project.executeTargets(Project.java:1189)
    [junit]     at org.apache.tools.ant.Main.runBuild(Main.java:758)
    [junit]     at org.apache.tools.ant.Main.startAnt(Main.java:217)
    [junit]     at org.apache.tools.ant.launch.Launcher.run(Launcher.java:257)
    [junit]     at org.apache.tools.ant.launch.Launcher.main(Launcher.java:104)

我的测试运行任务如下:

    <target name="run-junit-tests" depends="compile-tests,clean-results">
        <mkdir dir="${test.results.dir}"/>
        <junit failureproperty="tests.failed" fork="true" showoutput="yes" includeantruntime="false">
            <classpath refid="test.run.path" />
            <formatter type="xml" />
            <test name="project.AllTests" todir="${basedir}/test-results" />
        </junit>
        <fail if="tests.failed" message="Unit tests failed"/>
    </target>

我已经验证了类路径包含以下内容以及所有程序代码和库:

ant-junit.jar
ant-launcher.jar
ant.jar
easymock.jar
easymockclassextension.jar
junit-4.4.jar

我已经尝试调试以找出它试图打开哪个ZipFile没有运气,我已经尝试切换includeantruntimefork我已经尝试使用ant -lib test/libs运行ant,其中test/libs包含ant和junit库.

感谢有关导致此异常的原因或您如何配置ant以成功运行单元测试的任何信息.

ant 1.7.1(ubuntu),java 1.6.0_10,junit 4.4

谢谢.

更新 - 修复 发现我的问题.我使用文件集在我的路径中包含了我的类目录,而不是导致.class文件作为ZipFiles打开的​​文件,这当然引发了异常.

chi*_*tom 15

发现我的问题.我使用文件集在我的路径中包含了我的类目录,而不是导致.class文件作为ZipFiles打开的文件,这当然引发了异常.


小智 9

此错误是由于类路径包含对一个或多个不是JAR的[文件]的显式引用而引起的.对"打开zip文件中的错误"的引用当然是JAR实际上是一个ZIP文件,其他文件[JUNIT]发现类文件不是,因此没有zip格式.所以类路径应该只包含对JAR [files]的显式引用和/或其他资源(如类文件)的[目录]的名称.

因此,在构建类路径时(在ANT中)使用:

<path id="proj.class.path">
    <pathelement location="c:/my/project/root" />       :one for each of the [directories] where class files, log4j property files and other resources etc are to be found
    <fileset refid="my.file.set">                   :to describe all the explicit JAR [files] that need to be on the class path.
</path>
Run Code Online (Sandbox Code Playgroud)

哪里

<fileset id="my.file.set" dir="c:/where/i/keep/my/jars">
    <filename name="myjar1.jar" />
    <filename name="myjar2.jar" />
    <filename name="myjar3.jar" />
</fileset>
Run Code Online (Sandbox Code Playgroud)

要么

注意:使用通配符时[**/*],需要确保通配符不匹配非JAR文件的文件

<fileset id="my.file.set" dir="c:/where/i/keep/my/jars">
    <include name="**/*.jar" />
</fileset>
Run Code Online (Sandbox Code Playgroud)