我正试着用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
我试过调试以找出它试图打开哪个zip文件,但没有结果。
Ant 1.7.1(Ubuntu),java 1.6.0_10,JUnit 4.4
发布于 2018-03-01 09:06:35
发现了我的问题。在我的路径中包含了我的类目录,使用文件集相对于路径元素这导致.class文件作为ZipFiles打开,这当然会引发异常。
发布于 2018-03-01 09:36:43
此错误是由于类路径包含对一个或多个类的显式引用而引起的。
因此,在构建类路径时(在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>
其中
<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>
https://stackoverflow.com/questions/-100007460
复制相似问题