另一個經典的螞蟻問題。就像我在這個主題中發現的其他問題一樣,它可能是一些微妙的類路徑錯誤,但是沒有任何建議的解決方案適用於我。這裏是我的測試代碼,運行在Eclipse就好了 -NoClassDefFoundError for ant junit任務
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 \審判和源文件都在$ {WORKDIR} \ SRC \試行) -
<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>
我得到同樣的錯誤是否我運行在命令行或從日食螞蟻窗格中的螞蟻任務的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),但後來聲稱它h作爲錯誤的名字。任何建議感激地收到,特別是調試提示,所以我可以在將來避免這個問題。
我嘗試了一些文件路徑上的變化,他們沒有解決問題。我把testbin的值改回到普通的「bin」,它仍然聲稱它找不到測試。有沒有辦法讓我「迴盪」junit認爲它正在使用的類路徑? (順便說一句,編譯任務工作正常,班級文件顯示他們應該在哪裏。) – Sabrina
Arg。我還必須改變文件集include爲**/* Tests.class。現在很高興。謝謝! – Sabrina