本文介绍了配置 ant 以运行单元测试.图书馆应该在哪里?应该如何配置类路径?避免 ZipException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我的测试运行任务如下:

<target name="run-junit-tests";取决于=编译测试,清理结果"><mkdir dir="${test.results.dir}"/><junit failureproperty="tests.failed";叉=真"showoutput=是"includeantruntime=假"><classpath refid="test.run.path";/><格式化程序类型=xml"/><test name="project.AllTests";todir="${basedir}/test-results";/></junit><fail if="tests.failed";消息=单元测试失败"/></目标>

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

ant-junit.jarant-launcher.jar蚂蚁罐子easymock.jareasymockclassextension.jarjunit-4.4.jar

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

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

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

谢谢.

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

解决方案

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

I'm trying to run my junit tests using ant. The tests are kicked off using a JUnit 4 test suite. If I run this direct from Eclipse the tests complete without error. However if I run it from ant then many of the tests fail with this error repeated over and over until the junit task crashes.

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

my test running task is as follows:

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

I've verified that the classpath contains the following as well as all of the program code and libraries:

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

I've tried debugging to find out which ZipFile it is trying to open with no luck, I've tried toggling includeantruntime and fork and i've tried running ant with ant -lib test/libs where test/libs contains the ant and junit libraries.

Any info about what causes this exception or how you've configured ant to successfully run unit tests is gratefully received.

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

Thanks.

Update - FixedFound my problem. I had included my classes directory in my path using a fileset as opposed to a pathelement this was causing .class files to be opened as ZipFiles which of course threw an exception.

解决方案

Found my problem. I had included my classes directory in my path using a fileset as opposed to a pathelement this was causing .class files to be opened as ZipFiles which of course threw an exception.

这篇关于配置 ant 以运行单元测试.图书馆应该在哪里?应该如何配置类路径?避免 ZipException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 13:17