Configuring ant to run unit tests. Where should libraries be? How should classpath be configured? avoiding ZipException

后端 未结 5 1771
花落未央
花落未央 2021-02-05 08:18

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 i

相关标签:
5条回答
  • 2021-02-05 08:24

    This error is caused specifically because the class path contains explicit references to one or more [files] that are not JAR's. The reference to "error in opening zip file" is of course that a JAR is in effect a ZIP file where as other files [JUNIT] has found like class files are not and as such do not have a zip format. So the class path should contain only explicit references to JAR [files] and/or the names of the [directories] where other resources like class files are to be found.

    So when building up your class path (in ANT) use:

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

    where

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

    or

    NOTE: When using wild cards like [**/*] in you need to make sure the wild card is not matching files that are not JAR files

    <fileset id="my.file.set" dir="c:/where/i/keep/my/jars">
        <include name="**/*.jar" />
    </fileset>
    
    0 讨论(0)
  • 2021-02-05 08:32

    If you are using Ubuntu or Debian, this will make JUnit (and some other libs) always available for Ant:

    sudo apt-get install ant-optional
    
    0 讨论(0)
  • 2021-02-05 08:39

    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.

    0 讨论(0)
  • 2021-02-05 08:42

    Thanks guys for this information. I just want to add some tip from my experience. I have the same problem with junit as you when junit tryes to open license*.txt files in lib folder where *.jar resides.(Ivy resolve process puts them here) So

    <path id="lib.path.id">
        <fileset dir="${lib.dir}" includes="**.jar"/>
    </path>
    

    helps too.

    0 讨论(0)
  • 2021-02-05 08:49

    It sounds like there is an issue with paths.

    Check following error source:

    • classpath: print out the classpath variable in a junit test, run it from eclipse and ant, so you can compare them
    • Check your project for absolute paths. Probably, ant uses other path prefixes than eclipse.

    Some more information would help to help :)

    Good luck!

    0 讨论(0)
提交回复
热议问题