Ant JUnit ClassNotFoundException [英] Ant JUnit ClassNotFoundException

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

问题描述

我意识到有很多与这个类似的问题,然而,在阅读了其中的大部分内容后,我似乎遇到了同样的问题,但原因不同.

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.

我正在我的 Ant 构建中运行 任务,并且收到 ClassNotFoundException.在大多数类似相关的问题中(例如上面的问题),结果证明是作者在不同程度上没有正确配置 JUnit 类路径.虽然对我来说很可能是一样的,但对这些其他问题的任何建议都没有对我有用.

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.

我的项目目录结构:

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

src/test/java/FakeObjectUnitTest.java中:

// Package and imports omitted

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

        Assert.assertEquals(x, 5);
    }
}

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

我这样做是为了证明它在 JUnit 类路径上看到了 FakeObjectUnitTest.class.但是,它失败并出现错误.当我进入这个测试的 XML TEST 报告时,我看到以下内容:

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>

当我以详细模式 (-v) 运行 Ant 时,我看到的唯一提示是一堆警告:

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

忽略异常 java.util.zip.ZipException:打开 zip 文件读取资源 x 时出错

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

...其中 x 是类文件的名称.

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

我在 SO 上发现了 this 问题,结果证明问题是作者在类路径中添加了类文件,而不是 JAR(这让 JUnit 不高兴).所以我尝试从 junit.class/path 中删除 main.class.pathtest.class.path ,然后我得到一个异常,没有可以找到类文件.

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.

对正在发生的事情有任何想法或想法吗?提前致谢,对于 -v 问题很抱歉,但我认为细节越多越好.

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>

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

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