【问题标题】:ZipException when running junit tests运行junit测试时出现ZipException
【发布时间】:2011-05-16 09:01:29
【问题描述】:

我一直试图让 ant 执行一些用 junit 编写的测试,但徒劳无功。任何建议将不胜感激。我对 ant 和 Java 都很陌生,所以请耐心等待。

作为一个快速总结,我正在做的是试图让 ant 执行一个非常简单的测试,考虑到 ant -debug 的输出,类路径看起来不错。对于类路径中明确提到的类文件的测试,我收到一个空测试错误。另外,我收到一个 ZipException,我不知道这是怎么回事。

这是我正在尝试运行的测试用例:

package testmanagment;
import junit.framework.*;

public abstract class EasyTest extends TestCase
{

    public EasyTest(String name)
    {
        super(name);
    }

    protected void setUp()
    {}

    protected void testSeeMee() throws Exception
    {
        assertTrue(true);
    }

    protected void testSeeMeetoo() throws Exception
    {
        assertTrue(true);
    }   
}

包中有一些测试,这只是为了看看为什么一切都失败了。 ZipException 失败。

这是我的一些制作文件:

  <property name="src" value="src/"/> 
     <property name="build" value="build/"/>  
     <property name="test" value="${build}test/"/>
     <property name="testreportsdir" value="${test}reports/"/>
     <property name="classdir" value="${build}classes/"/>
     <property name="lib" value="lib/"/> 

    <path id="files-classpath">  
        <fileset dir="lib/" >  
            <include name="*.jar"/>  
        </fileset>  
    </path> 

    <path id="tests-classpath">
        <path refid="files-classpath"/>
        <pathelement location="${classdir}/"/>
    </path>

    <path id="tests-runpath">
        <path refid="tests-classpath"/>
        <fileset dir="${test}/">
            <include name="*.class"/>
            <include name="**/*.class"/>
            <include name="testmanagment/*.class"/>
            <include name="*.*"/>
            <include name="**/*.*"/>
            <include name="testmanagment/*.*"/>
        </fileset>
        </path>

blah blah blah

    <target name="test" depends="compile_tests">
        <junit haltonfailure="true"  printsummary="false">
            <classpath refid="tests-runpath"/>
            <formatter type="brief" usefile="false"/>
            <test name="testmanagment.EasyTest"/>
            <test name="testmanagment.TestUser"/>
            <test name="testmanagment.TestTest"/>
                </junit>
    </target>

ant 可以很好地编译所有内容,并希望将所有内容放在正确的位置。但它似乎找不到我的测试......这是我用 -debug 选项运行它时蚂蚁吐出的一小部分。

 [junit] Using CLASSPATH /host/Users/Sheena/Desktop/School/Software dev/lab_project/lib/junit-4.8.1.jar:/host/Users/Sheena/Desktop/School/Software dev/lab_project/lib/sqlitejdbc-v056.jar:/host/Users/Sheena/Desktop/School/Software dev/lab_project/build/classes:/host/Users/Sheena/Desktop/School/Software dev/lab_project/build/test/testmanagment/EasyTest.class:/host/Users/Sheena/Desktop/School/Software dev/lab_project/build/test/testmanagment/TestTest.class:/host/Users/Sheena/Desktop/School/Software dev/lab_project/build/test/testmanagment/TestUser.class:/usr/share/ant/lib/junit.jar:/usr/share/java/ant-launcher-1.7.1.jar:/usr/share/ant/lib/ant.jar:/usr/share/ant/lib/ant-junit.jar
Class org.apache.tools.ant.taskdefs.optional.junit.BriefJUnitResultFormatter loaded from parent loader (parentFirst)
Finding class testmanagment.EasyTest
Ignoring Exception java.util.zip.ZipException: error in opening zip file reading resource testmanagment/EasyTest.class from /host/Users/Sheena/Desktop/School/Software dev/lab_project/build/test/testmanagment/EasyTest.class
Ignoring Exception java.util.zip.ZipException: error in opening zip file reading resource testmanagment/EasyTest.class from /host/Users/Sheena/Desktop/School/Software dev/lab_project/build/test/testmanagment/TestTest.class
Ignoring Exception java.util.zip.ZipException: error in opening zip file reading resource testmanagment/EasyTest.class from /host/Users/Sheena/Desktop/School/Software dev/lab_project/build/test/testmanagment/TestUser.class
    [junit] Testsuite: testmanagment.EasyTest
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
    [junit] 
    [junit] Null Test:  Caused an ERROR

很抱歉那里的输出很丑,这是我现在必须处理的全部内容。

它看起来很像测试用例在类路径中,所以我不知道发生了什么......也许它与 ZipException 有关但我不知道......

【问题讨论】:

  • 您使用什么工具?您是否使用任何带有 ANT 插件的 IDE,例如 Netbeans 或 Eclipse?如果路径错误,它们应该生成警告。

标签: java exception ant junit classpath


【解决方案1】:

您已将 EasyTest.class 作为 JAR 文件添加到类路径中。这行不通。类文件不是 JAR 归档文件,因此类加载器在尝试从中加载类时会引发错误。

【讨论】:

  • 不,我使用: 来包含所有 .class 文件,除非我还应该在其他地方声明它?看起来 ant 知道它是一个 .class
  • 您不能将类文件添加到类路径中。只添加包含包的目录。也就是说,如果您的班级是testmanagment.EasyTest,请添加包含 testmanagment/ 的文件夹,因为Java 会将包名添加到搜索路径中。
  • 我尝试过使用文件集,无论我使用 *.class 还是仅使用目录,它都会给我 zipexception。而且我尝试了类似的路径元素的东西,但它只是找不到文件。天哪,这有点老了。一定是打错了,就是找不到。
  • 不要使用fileset,使用&lt;pathelement location="${classdir}"/&gt;
  • &lt;dirset&gt; 也适用于文件集失败的地方,当您将目录添加到类路径时(但路径/路径元素更“现代”)
【解决方案2】:

我假设您正在运行 jUnit 3。您可能想尝试创建一个不是抽象类的 TestCase 类。另外,要有一个默认的构造函数,否则 jUnit 将不知道如何创建测试类。

在你的情况下应该是:

package testmanagment; 

import junit.framework.*;

public class EasyTest extends TestCase {

    public EasyTest() {
        super("Easy Test");
    }

    public void setUp() {
    }

    public void testSeeMee() throws Exception {
        assertTrue(true);
    }

    public void testSeeMeetoo() throws Exception {
        assertTrue(true);
    }   
}

【讨论】:

  • 我正在使用 3.8.2。取出 abstract 关键字不会改变任何事情
  • 去掉构造函数怎么样?
猜你喜欢
  • 2016-02-15
  • 1970-01-01
  • 2012-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多