蚂蚁的JUnit的ClassNotFoundException [英] Ant JUnit ClassNotFoundException

查看:249
本文介绍了蚂蚁的JUnit的ClassNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多类似的问题作为这个,读数超过他们大多不过之后我似乎有同样的问题,但出于不同的原因。

我运行一个< JUnit的> 任务我的Ant构建内,我得到一个的ClassNotFoundException 。在大多数的类似相关的问题(比如上面的那个),这竟然是不同程度的作者根本就没有正确配置JUnit的类路径。虽然这很可能是同一对我来说,没有任何的建议对上述任何一个其他的问题都为我工作。

我的项目目录结构:

  MyProject的/
    钢骨混凝土/主/ JAVA /
        FakeObject.java
        ......我的Java主要来源其余
    SRC / test / java下
        FakeObjectUnitTest.java
        ......我的Java测试源的其余部分
    斌/主
        FakeObject.class
        ...主要的Java二进制文件的剩余部分
    斌/测试
        FakeObjectUnitTest.class
        ...主要的Java二进制文件的剩余部分
    LIB /主
        ......我的主要依赖
    LIB /测试
        基于JUnit 4.10.jar
        hamcrest-1.1.jar
        ......我的测试依赖的其余部分
    根/报道
        ...其中的JUnit应该把所有的测试报告
    建立/
        build.xml文件
        build.properties

的src /测试/ JAVA / FakeObjectUnitTest.java

  //包装,进口略公共类FakeObjectUnitTest {
    @测试
    公共无效DoSomething的(){
        INT X = 5;        Assert.assertEquals(X,5);
    }
}

我的的build.xml

 <项目名称=BASEDIR =...>
    <! - 主类路径。 - >
    &所述;路径ID =main.class.path>
        <文件集DIR =斌/主/>
    < /路径>    <! - 测试类路径。 - >
    &所述;路径ID =test.class.path>
        <文件集DIR =斌/测试/>
    < /路径>    <! - JUnit的类路径。 - >
    &所述;路径ID =junit.class.path>
        <文件集DIR =LIB /主包括=*罐子。/>
        <文件集DIR =LIB /测试包括=*罐子。/>
        <路径REFID =main.class.path/>
        <路径REFID =test.class.path/>
    < /路径>    <! -
        所有previous目标为简洁起见省略(如为编译,等),但
    我向你们保证,他们的工作和编译FakeObject.java/FakeObjectUnitTest.java成
    分别FakeObject.class / FakeObjectUnitTest.class,并将它们放置在正确的
的bin目录。
     - >    <目标名称=运行JUnit的依赖=编译>        <属性名=junit.pathREFID =junit.class.path/>
        <回声消息=JUnit的类路径是:$ {} junit.path/>        < JUnit的printsummary =YEShaltonerror =YEShaltonfailure =YES>
            <类路径REFID =junit.class.path/>            <格式化类型=XML/>            < batchtest todir =根/报道>
                <文件集DIR =的src / test / java下>
                    <包括姓名=** / * *的UnitTest的.java/>
                < /文件集>
            < / batchtest>
        < / JUnit的>
    < /目标与GT;
< /项目>

当我在命令行运行这个目标:

 运行JUnit的:
    [回应]导电单元测试使用JUnit。
    [回应] JUnit的类路径是: /<path-to-my-project>/MyProject/lib/test/hamcrest-1.1.jar:/<path-to-my-project>/MyProject/lib/test/junit-4.10.jar:/<path-to-my-project>/MyProject/bin/main/com/myproject/client/FakeObject.class:/<path-to-my-project>/MyProject/bin/test/com/myproject/client/FakeObjectUnitTest.class
    [JUnit的]运行com.myproject.client.FakeObjectUnitTest
    [JUnit的]测试运行:1,失败:0,错误:1,经过时间:0秒

我这样做是为了的证明的,它认为 FakeObjectUnitTest.class 在JUnit的类路径中。然而,它不能用一个错误。当我进入本次测试的XML测试报告,我看到以下内容:

 &LT;错误信息=com.myproject.client.FakeObjectUnitTestTYPE =抛出java.lang.ClassNotFoundException&GT;抛出java.lang.ClassNotFoundException:com.myproject.client.FakeObjectUnitTest
    在java.net.URLClassLoader的$ 1.run(URLClassLoader.java:202)
    在java.security.AccessController.doPrivileged(本机方法)
    在java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    在java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    在java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    在java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    在java.lang.Class.forName0(本机方法)
    在java.lang.Class.forName(Class.java:247)
&LT; /错误&GT;

当我在详细模式下运行Ant( -v )的唯一的提示的我看到的是一堆的线沿线的警告:


  

忽略异常java.util.zip.ZipException:错误打开zip文件中读取资源X


...其中 X 是类文件的名称。

我发现这个问题在SO,问题竟然是那笔者又增加了类文件到classpath,而不是JAR文件(这使得JUnit的不快)。所以,我想从的JUnit删除 main.class.path test.class.path 。类/路径,然后我得到一个异常,没有一个类文件中可以找到。

任何意见或想法,以什么回事?在此先感谢,并为这里的 -v 问题抱歉,但我想通了更多的细节,就更好了。


解决方案

您需要使用的包状结构:结果
从手动


  

无论路径状值需要被指定,可以使用一个嵌套元件。这需要的一般形式:

 &LT;&类路径GT;
      &LT; pathelement PATH =$ {}类路径/&GT;
      &LT; pathelement位置=LIB / helper.jar/&GT;
    &LT; /类路径&GT;


所以,你的解决办法如下:

 &LT;! -  JUnit的类路径。 - &GT;
&所述;路径ID =junit.class.path&GT;
    &LT;文件集DIR =LIB /主包括=*罐子。/&GT;
    &LT;文件集DIR =LIB /测试包括=*罐子。/&GT;
    &LT; pathelement位置=斌/主/&GT;
    &LT; pathelement位置=斌/测试/&GT;
&LT; /路径&GT;

I realize there are a lot of similar questions as this one, however after reading over most of them I seem to be having the same problem, but for different reasons.

I'm running a <junit> task inside my Ant build and am getting a ClassNotFoundException. In most of the similarly-related questions (such as the one above), this turned out to be various degrees of the author simply not configuring the JUnit classpath correctly. Although it may very well be the same for me, none of the suggestions to any of these other questions have worked for me.

My project directory structure:

MyProject/
    src/main/java/
        FakeObject.java
        ...the rest of my Java main source
    src/test/java
        FakeObjectUnitTest.java
        ...the rest of my Java test source
    bin/main
        FakeObject.class
        ...the rest of main Java binaries
    bin/test
        FakeObjectUnitTest.class
        ...the rest of main Java binaries
    lib/main
        ...my main dependencies
    lib/test
        junit-4.10.jar
        hamcrest-1.1.jar
        ...the rest of my test dependencies
    gen/reports
        ...where JUnit should be placing all test reports
    build/
        build.xml
        build.properties

In src/test/java/FakeObjectUnitTest.java:

// Package and imports omitted

public class FakeObjectUnitTest {
    @Test
    public void doSomething() {
        int x = 5;

        Assert.assertEquals(x, 5);
    }
}

My build.xml:

<project name="" basedir="..">
    <!-- Main classpath. -->
    <path id="main.class.path">
        <fileset dir="bin/main"/>
    </path>

    <!-- Test classpath. -->
    <path id="test.class.path">
        <fileset dir="bin/test"/>
    </path>

    <!-- JUnit classpath. -->
    <path id="junit.class.path">
        <fileset dir="lib/main" includes="*.jar"/>
        <fileset dir="lib/test" includes="*.jar"/>
        <path refid="main.class.path"/>
        <path refid="test.class.path"/>
    </path>

    <!--
        All previous targets omitted for brevity (such as for compiling, etc.), but
    I assure you all that they work and compile FakeObject.java/FakeObjectUnitTest.java into
    FakeObject.class/FakeObjectUnitTest.class respectively, and place them in the correct
bin directories.
    -->

    <target name="run-junit" depends="compile">

        <property name="junit.path" refid="junit.class.path"/>
        <echo message="JUnit ClassPath is: ${junit.path}"/>

        <junit printsummary="yes" haltonerror="yes" haltonfailure="yes">
            <classpath refid="junit.class.path"/>

            <formatter type="xml"/>

            <batchtest todir="gen/reports">
                <fileset dir="src/test/java">
                    <include name="**/*UnitTest*.java"/>
                </fileset>
            </batchtest>
        </junit>
    </target>
</project>

When I run this target from the command-line:

run-junit:
    [echo] Conducting unit tests with JUnit.
    [echo] JUnit ClassPath is: /<path-to-my-project>/MyProject/lib/test/hamcrest-1.1.jar:/<path-to-my-project>/MyProject/lib/test/junit-4.10.jar:/<path-to-my-project>/MyProject/bin/main/com/myproject/client/FakeObject.class:/<path-to-my-project>/MyProject/bin/test/com/myproject/client/FakeObjectUnitTest.class
    [junit] Running com.myproject.client.FakeObjectUnitTest
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec

I do this to prove that it sees FakeObjectUnitTest.class on the JUnit classpath. However, it fails with an error. When I go into the XML TEST report for this test, I see the following:

<error message="com.myproject.client.FakeObjectUnitTest" type="java.lang.ClassNotFoundException">java.lang.ClassNotFoundException: com.myproject.client.FakeObjectUnitTest
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:247)
</error>

When I run Ant in verbose mode (-v) the only hint I see are a bunch of warnings along the lines of:

Ignoring Exception java.util.zip.ZipException: error in opening zip file reading resource x

...where x is the name of a class file.

I found this question on SO, and the problem turned out to be that the author had added class files to the classpath, not JARs (which makes JUnit unhappy). So I tried removing main.class.path and test.class.path from junit.class/path and then I get an exception that none of the class files can be found.

Any ideas or thoughts as to what is going on? Thanks in advance, and sorry for the -v question here, but I figured the more details, the better.

解决方案

You need to use for package-like structures:
From the manual:

Wherever path-like values need to be specified, a nested element can be used. This takes the general form of:

    <classpath>
      <pathelement path="${classpath}"/>
      <pathelement location="lib/helper.jar"/>
    </classpath>

So your solution would look like:

<!-- JUnit classpath. -->
<path id="junit.class.path">
    <fileset dir="lib/main" includes="*.jar"/>
    <fileset dir="lib/test" includes="*.jar"/>
    <pathelement location="bin/main"/>
    <pathelement location="bin/test"/>
</path>

这篇关于蚂蚁的JUnit的ClassNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆