【问题标题】:NoClassDefFoundError for ant junit taskant junit 任务的 NoClassDefFoundError
【发布时间】:2011-11-07 18:34:04
【问题描述】:

另一个经典的 ant junit 问题。就像我在该主题上发现的其他问题一样,这可能是一些微妙的类路径错误,但没有一个建议的解决方案对我有用。这是我的测试代码,在 Eclipse 中运行良好 --

package trial;

import java.net.MalformedURLException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import trial.CommonCode;  //this is a library of methods, since the test runs I'm not including it here

public class BaseTests {
    WebDriver driver;
    CommonCode myCommon;

    @Before 
    public void startup() throws MalformedURLException, InterruptedException{
        myCommon=new CommonCode();
        driver=myCommon.driver;
    }

    @After
    public void cleanup(){
        driver.quit();
    }   

    @Test
    public void deleteLists(){
        System.out.println("Hi Mom!");
        myCommon.loginLibrary("GAL");

    }
}

这是我的 build.xml(类文件在 ${workdir}\bin\trial 中,源文件在 ${workdir}\src\trial 中)--

<project default="main" basedir=".">

    <property name="testbin" value="bin\trial"/>
    <property name="src.dir" value="src\trial"/>

    <path id="path.class">
      <pathelement location="c:\Java\selenium-2.8.0" /> 
      <pathelement location="C:\eclipse\plugins\org.junit_4.8.2.v4_8_2_v20110321-1705"/>
        <pathelement location="C:\eclipse\plugins\org.apache.ant_1.8.2.v20110505-1300"/>
      <fileset dir="c:\Java\selenium-2.8.0" includes="*.jar" /> 
      <fileset dir="C:\eclipse\plugins\org.junit_4.8.2.v4_8_2_v20110321-1705" includes="*.jar"/>
        <fileset dir="C:\eclipse\plugins\org.apache.ant_1.8.2.v20110505-1300\lib" includes="*.jar"/>
      <path refid="src.dir" /> 
      </path>

    <path id="src.dir">
      <pathelement location="${src.dir}" /> 
      </path>

    <path id="test.dir">
        <pathelement location="${testbin}"/>
    </path>    

    <target name="main" depends="compile" description="main target">
        <echo> Building now </echo>
    </target>

    <target name="compile" description="compilation target" >
        <javac srcdir="${src.dir}" destdir="${testbin}" classpathref="path.class" debug="on" verbose="true"/>
    </target>

    <target name="runtest" >
            <junit fork="no" printsummary="true" showoutput="true">
                <classpath>
                    <path refid="path.class"/>
                    <path refid="test.dir"/>
                    <path refid="src.dir"/>
                </classpath>
                <formatter type="brief" usefile="false" />
                <batchtest fork="yes">
                    <fileset dir="${testbin}">                  
                        <include name="*Tests.class"/>
                    </fileset>
                </batchtest>

            </junit>

        </target>

</project>

无论我是从命令行还是从 eclipse ant 窗格运行 ant 任务 runtest,我都会遇到相同的错误:

runtest:
    [junit] Running BaseTests
    [junit] Testsuite: BaseTests
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
    [junit] Null Test:  Caused an ERROR
    [junit] BaseTests (wrong name: trial/BaseTests)
    [junit] java.lang.NoClassDefFoundError: BaseTests (wrong name: trial/BaseTests)
    [junit]     at java.lang.ClassLoader.defineClass1(Native Method)
    [junit]     at java.lang.ClassLoader.defineClassCond(Unknown Source)
    [junit]     at java.lang.ClassLoader.defineClass(Unknown Source)
    [junit]     at java.security.SecureClassLoader.defineClass(Unknown Source)
    [junit]     at java.net.URLClassLoader.defineClass(Unknown Source)
    [junit]     at java.net.URLClassLoader.access$000(Unknown Source)
    [junit]     at java.net.URLClassLoader$1.run(Unknown Source)
    [junit]     at java.security.AccessController.doPrivileged(Native Method)
    [junit]     at java.net.URLClassLoader.findClass(Unknown Source)
    [junit]     at java.lang.ClassLoader.loadClass(Unknown Source)
    [junit]     at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    [junit]     at java.lang.ClassLoader.loadClass(Unknown Source)
    [junit]     at java.lang.Class.forName0(Native Method)
    [junit]     at java.lang.Class.forName(Unknown Source)
    [junit] Test BaseTests FAILED

所以它找到了正确的 junit 测试 (trial.BaseTests),但随后声称它的名称错误。任何建议都非常感谢,尤其是调试提示,这样我以后可以避免这个问题。

【问题讨论】:

    标签: ant junit


    【解决方案1】:

    首先,你的 src 目录应该是 src 并且你的 testbin 应该是 bin。这些是你的包树的根。

    接下来,类路径应该包含 jar 文件和包含已编译类的目录,而不是源文件。所以 junit 的类路径应该包含 testdir,而不是 src 目录。

    最后,您应该使用正斜杠而不是反斜杠,即使在 Windows 上也是如此,并且在定义保存文件或目录路径的属性时,您还应该使用 location 属性而不是 value 属性。

    【讨论】:

      【解决方案2】:

      您的文件路径似乎不正确。它们应该与包名称匹配。

      您的测试类是 trial.BaseTests。您是在告诉 JUnit 执行 bin/trial 下的所有测试。于是,它搜索了 bin/trial 下的所有文件,找到了 BaseTests.class,它应该在 trial/BaseTests.class 下。

      将您的测试箱从“bin/trial”更改为“bin”:

      <property name="testbin" value="bin"/>
      

      【讨论】:

      • 我在文件路径上尝试了多种变体,但都没有解决问题。我将 testbin 值改回普通的“bin”,但它仍然声称找不到测试。有没有办法让我“回显”junit认为它正在使用的类路径? (顺便说一句,编译任务工作正常,类文件显示在它们应该出现的位置。)
      • 精氨酸。我还必须将文件集包含更改为 **/*Tests.class。现在它很高兴。谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-11
      • 2011-10-05
      • 2012-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多