Wednesday, January 28, 2009

jUnit / jMock: error in opening zip file

I was getting an exception using jUnit / jMock tests together with Ant:

  [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.(AntClassLoader.java:109)
  [junit] at org.apache.tools.ant.AntClassLoader.findResources(AntClassLoader.java:975)
  [junit] at java.lang.ClassLoader.getResources(ClassLoader.java:1016)
  [junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.checkForkedPath(JUnitTask.java:1126)
  [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.NativeMethodAccessorImpl.invoke0(Native Method)
  [junit] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
  [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)




Solution:

Use

    <path id="classpath.run.tests"> 
        <pathelement location="." />                                                                 
    </path>


instead of

    <path id="classpath.run.tests">                                                                  
        <fileset dir=".">                                                                            
            <include name="*.class" />                                                               
        </fileset>                                                                                   
    </path>


The explanation.