Java 配置ant以运行单元测试。图书馆应该在哪里?类路径应该如何配置?避免ZipException

Java 配置ant以运行单元测试。图书馆应该在哪里?类路径应该如何配置?避免ZipException,java,ant,junit,Java,Ant,Junit,我正在尝试使用ant运行junit测试。测试使用JUnit4测试套件开始。如果我直接从Eclipse运行这个测试,那么测试将毫无错误地完成。然而,如果我从ant运行它,那么许多测试都会失败,这个错误会一次又一次地重复,直到junit任务崩溃 [junit] java.util.zip.ZipException: error in opening zip file [junit] at java.util.zip.ZipFile.open(Native Method) [j

我正在尝试使用ant运行junit测试。测试使用JUnit4测试套件开始。如果我直接从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) [junit]java.util.zip.ZipException:打开zip文件时出错 [junit]位于java.util.zip.ZipFile.open(本机方法) [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.UnknowneElement.execute(unknowneElement.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" 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 ant-junit.jar ant-launcher.jar 蚂蚁罐 easymock.jar easymockclassextension.jar junit-4.4.jar 我尝试过调试,以找出它试图打开的ZipFile没有运气,我尝试过切换includeantruntime和fork,我尝试过用ant-lib test/libs运行ant,其中test/libs包含ant和junit库

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

Ant1.7.1(ubuntu)、Java1.6.0_10、JUnit4.4

谢谢

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

听起来路径有问题

检查以下错误源:

  • classpath:在junit测试中打印出classpath变量,从eclipse和ant运行它,以便比较它们
  • 检查项目中的绝对路径。ant可能使用eclipse以外的其他路径前缀
更多信息将有助于帮助:)


祝你好运

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

感谢各位提供的信息。我只是想从我的经验中补充一些建议。当junit尝试在*.jar所在的lib文件夹中打开许可证*.txt文件时,我和您有同样的问题。(Ivy解析过程将它们放在这里)所以



也有帮助

此错误是由于类路径包含对一个或多个非JAR的[文件]的显式引用而导致的。“打开zip文件时出错”的引用当然是JAR实际上是一个zip文件,正如其他文件[JUNIT]发现的那样,类文件不是,因此没有zip格式。因此,类路径应该只包含对JAR[files]和/或[directory]的显式引用,在这些目录中可以找到类文件等其他资源

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


如果您使用的是Ubuntu或Debian,这将使JUnit(以及其他一些LIB)始终可用于Ant:

sudo apt-get install ant-optional
<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>
<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>
<fileset id="my.file.set" dir="c:/where/i/keep/my/jars">
    <include name="**/*.jar" />
</fileset>
sudo apt-get install ant-optional