运行 junit 测试时出现 ZipException

发布于 2024-11-07 14:59:21 字数 4230 浏览 0 评论 0原文

我一直徒劳地尝试让 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。

这是我的 make 文件的一小部分:

  <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 选项运行它时 ant 吐出的一小部分内容。

 [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 有关,但我不知道...

I've been trying in vain to get ant to execute some tests written in junit. Any advice would be much appreciated. I'm pretty new to both ant and Java so please be patient.

As a quick summary, what i'm doing is trying to get ant to execute a very simple test, the classpath looks ok considering the output from ant -debug. I'm getting a null test error for a test who's class file is explicitly mentioned in the classpath. Also, i'm getting a ZipException, i dont know what that's about.

Here's a testcase i'm trying to run:

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);
    }   
}

There are a few tests in the package, this one was just to see why everything was failing. it fails with ZipException.

and here's a little bit of my make file:

  <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 compiles everything fine, and looks to put everything in the right places. but it cant seem to find my tests... here's a little piece of what ant spat out when i ran it with the -debug option.

 [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

sorry about the ugly output there, it's all i have to work with for now.

it looks very much like the testcases are in the classpath, so i dont know what's happening... Maybe it's something to do with the ZipException but i dont know...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南风几经秋 2024-11-14 14:59:21

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

You added EasyTest.class as a JAR file to the classpath. This doesn't work. Class files aren't JAR archives, so the classloader throws an error when it tries to load classes from it.

拒绝两难 2024-11-14 14:59:21

我假设您正在运行 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);
    }   
}

I'm assuming you're running jUnit 3. You may want to try creating a TestCase class that isn't abstract class. Also, have a default constructor or else jUnit won't know how to create the test class.

In your case it should be:

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);
    }   
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文