配置ant以运行unit testing。 图书馆应该在哪里? 应该如何配置classpath? 避免ZipException

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

     [junit] java.util.zip.ZipException:在java.util.zip.ZipFile上java.util.zip.ZipFile.open(本机方法)[junit]打开zip文件[junit]时出错。(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] atg.apache.tools.ant.AntClassLoader $ ResourceEnumeration.nextElement(AntClassLoader.java:130)[junit] at org.apache.tools .ant.util.CollectionUtils $ CompoundEnumeration.nextElement(CollectionUtils.java:198)[junit] at the sun.misc.CompoundEnumeration.nextElement(CompoundEnumeration.java:43)[junit] at org.apache.tools.ant.taskdefs.optional .junit.JUnitTask.checkForkedPath(JUnitTask.java:1128)[junit] atg.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeAsForked(JUnitTask.java:1013)[junit] at org.apache.tools .ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTas  k.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 the 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] org.apache.tools.ant.Project.executeTarget(Project.java:1306)[junit] at org.apache.tools.ant.helper.DefaultExecu  tor.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) 

我的测试运行任务如下:

     
         
         
             
             
             
         
         
     

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

ant的junit.jar
antlauncher.jar
的ant.jar
 easymock.jar
 easymockclassextension.jar
 JUnit的-4.4.jar 

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

感谢有关导致此exception的原因或您如何配置ant以成功运行unit testing的任何信息。

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

谢谢。

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

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

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

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

  :one for each of the [directories] where class files, log4j property files and other resources etc are to be found  :to describe all the explicit JAR [files] that need to be on the class path.  

哪里

      

要么

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

    

听起来路径有问题。

检查以下错误来源:

  • classpath:在junit测试中打印出classpath变量,从eclipse和ant运行它,这样你就可以比较它们
  • 检查项目的绝对路径。 可能,ant使用其他路径前缀而不是eclipse。

一些更多的信息将有助于帮助:)

祝你好运!

谢谢你们这些信息。 我只是想从我的经验中添加一些提示。 当junit尝试在* .jar所在的lib文件夹中打开许可证* .txt文件时,我和junit有同样的问题。(常春藤解析进程将它们放在这里)所以

    

也有帮助。

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

 sudo apt-get install ant-optional