2014-07-18 99 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

相关问题