java - Ant JUnit ClassNotFoundException

标签 java ant junit classpath

我意识到有很多类似的问题 this one ,但是在阅读了其中的大部分之后,我似乎遇到了同样的问题,但原因不同。

我正在运行 <junit>在我的 Ant 构建中执行任务并得到 ClassNotFoundException .在大多数类似相关的问题中(例如上面的那个),事实证明这是作者不同程度的简单地没有正确配置 JUnit 类路径。尽管对我来说可能完全一样,但对这些其他问题的任何建议都对我没有用。

我的项目目录结构:

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>

当我从命令行运行这个目标时:

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

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

<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 时,我看到的唯一提示是一堆警告:

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

...哪里x是类文件的名称。

我找到了 this关于 SO 的问题,问题原来是作者在类路径中添加了类文件,而不是 JAR(这让 JUnit 不高兴)。所以我尝试删除 main.class.pathtest.class.path来自 junit.class/path然后我得到一个异常,找不到任何类文件。

关于正在发生的事情有什么想法或想法吗?在此先感谢,并对-v表示抱歉问题在这里,但我认为越详细越好。

最佳答案

你需要使用类包结构:
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>

所以你的解决方案看起来像:

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

关于java - Ant JUnit ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12745465/

相关文章:

java - 解析正则表达式日期

swing - 使用 Eclipse 的 JNLP 文件

java - 在 Zxing 中启用 PDF417 解码

java - Ant 构建失败 - 未设置 libs.CopyLibs.classpath 属性

java - EasyMock 和返回泛型类的方法

java - 如何使用 Spring jdbc 模板(jdbcTemplate 或 namedParameterJDBCTem)从数据库中检索值

java - 在java中,如果类的方法、静态变量和变量被多次实例化,就像多次实例化类一样

java - Eclipse 默认构建和 ant 构建

junit - Jacoco Sling Junit 集成 - 测试执行

java - Java 测试是否有某种 "hidden @Ignore"