2014-07-18 96 views
0

我試圖創造的理念,爲實際應用的證明時,該任務是不添加的東西對全球環境運行ant-JUnit測試。到目前爲止,我已經在螞蟻中實現了它,但是,我堅持以下例外。 ClassTest.java是具有樣本單元測試用例的java類。 (Junit 3.0風格)。我不確定爲什麼ant-junit沒有在批處理任務中提到路徑時找不到該類。的ClassNotFoundException - 運行ant-的JUnit

項目結構

enter image description here

我運行螞蟻junit任務時,出現以下情況例外。

Testsuite: ClassTest 
Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec 

    Caused an ERROR 
ClassTest 
java.lang.ClassNotFoundException: ClassTest 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247) 
    at java.lang.Class.forName0(Native Method) 
    at java.lang.Class.forName(Class.java:249) 
    at org.eclipse.ant.internal.launching.remote.EclipseSingleCheckExecutor.executeTargets(EclipseSingleCheckExecutor.java:30) 
    at org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32) 
    at org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:424) 
    at org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:138) 

我的Ant build.xml文件是

<project name="Java project build" default="build"> 
<property name="project.local.directory" value="C:\Users\usrpao\workspace\Ant" /> 
<property name="src.path" value="${project.local.directory}/src" /> 
<property name="lib.path" value="${project.local.directory}/lib" /> 
<property name="dest.path" value="${project.local.directory}/target" /> 
<property name="junit.output.dir" value="${project.local.directory}/junit" /> 

<path id="classpath.test"> 
    <fileset dir="lib"> 
     <include name="**/*.jar" /> 
    </fileset> 
</path> 

<!--<taskdef name="junit" classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask"> 
    <classpath refid="classpath.test" /> 
</taskdef> --> 

<path id="MyProject.classpath"> 
    <pathelement location="lib/ant-junit.jar" /> 
    <pathelement location="lib/junit.jar" /> 
     <pathelement location="bin/*.*"/> 
</path> 

<path id="lib.classpath"> 
    <pathelement location="lib/ant-junit.jar" /> 
    <pathelement location="lib/junit.jar" /> 
</path> 

<target name="build" depends="clean"> 
    <javac srcdir="${src.path}" destdir="${dest.path}" classpathref="lib.classpath"> 
    </javac> 
    <antcall target="test"> 
    </antcall> 
</target> 

<target name="clean"> 

</target> 

<target name="test"> 
    <pathconvert property="testoutput" refid="classpath.test" /> 
    <echo>Path = ${testoutput}</echo> 
    <mkdir dir="${junit.output.dir}" /> 
    <junit haltonerror="false" showoutput="true"> 

     <classpath refid="MyProject.classpath"> 
     </classpath> 
     <batchtest todir="${junit.output.dir}"> 
      <formatter type="plain" /> 
      <fileset dir="${src.path}/com/ant/test"> 
       <include name="**/*Test*.java" /> 
      </fileset> 
     </batchtest> 

    </junit> 
</target> 

+0

爲什麼在'MyProject.classpath'中包含''?它不應該是''而不是? – manouti

+0

@manouti我會嘗試 – Zeus

+0

@manouti沒有工作。 – Zeus

回答

1

你的類路徑引用bin目錄。你應該改變MyProject.classpath參考包括${project.local.directory}/target

<path id="MyProject.classpath"> 
    <pathelement location="lib/ant-junit.jar" /> 
    <pathelement location="lib/junit.jar" /> 
    <dirset dir="${project.local.directory}"> 
     <include name="target"/> 
    </dirset> 
</path> 

此外,你應該通過刪除相應的測試類的包名的文件夾改變junit任務裏面的fileset元素,否則你仍然會得到一個ClassNotFoundException

<junit haltonerror="false" showoutput="true"> 

    <classpath refid="MyProject.classpath"> 
    </classpath> 
    <batchtest todir="${junit.output.dir}"> 
     <formatter type="plain" /> 
     <fileset dir="${src.path}"> <!-- remove com/ant/test corresponding to package name --> 
      <include name="**/*Test*.java" /> 
     </fileset> 
    </batchtest> 

</junit> 
+0

Genious!這工作..感謝一個TON1 – Zeus

+0

可以請你看看這裏的問題http://stackoverflow.com/questions/24915142/classnotfoundexception-when-running-ant-junit – Zeus

0

從你的文件夾結構,顯而易見的是,該類ClassTest.classtarget文件夾中。所以在class-path你應該包括以下內容:

<pathelement path="target"/> 

希望這有助於。而類實際上是target目錄下

+0

這並不工作,我把文件夾在<路徑ID = 「MyProject.classpath」> \t \t \t \t \t \t \t path classpath元素,沒有工作。 – Zeus

相關問題