ant junit任务的NoClassDefFoundError

时间:2011-11-07 18:34:04

标签: ant junit

另一个经典的蚂蚁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),但后来声称它的名字错了。感激地收到任何建议,特别是调试提示,以便将来可以避免这个问题。

2 个答案:

答案 0 :(得分:1)

首先,您的src目录应为src,而您的testbin应为bin。这些是包树的根源。

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

最后,你应该使用正斜杠而不是反斜杠,即使在Windows上,你也应该在定义包含文件或目录路径的属性时使用location属性而不是value属性。

答案 1 :(得分:0)

您的文件路径看起来不正确。它们应与包名匹配。

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

将您的testbin从'bin / trial'更改为'bin':

<property name="testbin" value="bin"/>
相关问题