2011-11-14 150 views
1

我正在使用Eclipse Indigo。不知道发生了什么,但是我试图从ant构建中调用测试套件,我得到了一个ClassNotFoundException异常。但是,如果我右键单击JUnit Test类并以Junit Test身份运行,它将正常运行测试。该错误表示找不到文件./test/_ObservableSortUnitTests。即使给出文件的完整路径,它也会给出相同的错误。JUnit测试套件和Ant构建“ClassNotFoundException”

这是我的错误:

Buildfile: /home/jason/Dev/ObservableSort/build.xml 
Compile: 
Test: 
    [junit] Testsuite: ./test/_ObservableSortUnitTests 
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec 
    [junit]  Caused an ERROR 
    [junit] ./test/_ObservableSortUnitTests 
    [junit] java.lang.ClassNotFoundException: ./test/_ObservableSortUnitTests 
    [junit]  at java.lang.Class.forName0(Native Method) 
    [junit]  at java.lang.Class.forName(Class.java:264) 
    [junit]  at org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32) 
    [junit]  at org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:424) 
    [junit]  at org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:138) 

BUILD FAILED 
/home/jason/Dev/ObservableSort/build.xml:75: Test ./test/_ObservableSortUnitTests failed 

Total time: 1 second 

这里是我的ant脚本(声明:我是很新的蚂蚁):

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<project default="Build" name="CS 151 Project Build Script" > 
    <!--ANT 1.7 is required --> 
    <property name="home" value="." />     
    <property name="src.dir" value = "${home}/src" /> 
    <property name="dest.dir" value="${home}/Release" /> 
    <property name="dir.build" value="${home}/lib" /> 
    <property name="dir.javadoc" value="${dest.dir}/Javadoc" /> 
    <property name="dir.classes" value="${dest.dir}/Classes" /> 
    <property name="dir.junit.reports" value="${dest.dir}/Reports" /> 
    <property name="test.suite.dir" value="${home}/test" /> 
    <property name="test.suite.class" value ="${test.suite.dir}/_ObservableSortUnitTests" /> 

    <path id="build.class.path"> 
     <fileset dir="${dir.build}"> 
      <include name="*.jar" /> 
     </fileset> 
    </path> 

    <path id="test.class.path"> 
     <pathelement location="${junit.test.suite}" /> 
    </path> 

    <target name="Clean" description="Deletes all old files"> 
     <delete dir="${dir.javadoc}" /> 
     <delete dir="${dir.classes}" /> 
     <delete dir="${dir.junit.reports}" /> 
    </target> 

    <target name="Prepare" description="Creates all necessary directories"> 
     <mkdir dir="${dir.javadoc}" /> 
     <mkdir dir="${dir.classes}" /> 
     <mkdir dir="${dir.junit.reports}" /> 
    </target> 

    <target name="Compile"> 
     <javac srcdir="${src.dir}" destdir="${dir.classes}" includeantruntime="true"> 
      <classpath refid="build.class.path" /> 
     </javac> 
    </target> 

    <target name="Full" description="Executes all build targets"> 
     <antcall target="Clean" /> 
     <antcall target="Prepare" /> 
     <antcall target="Compile" /> 
     <antcall target="Test" /> 
     <antcall target="Build" /> 
     <antcall target="Javadoc" /> 
     <antcall target="run" /> 
    </target> 

    <target name="Build" description="Creates executable jar" depends="Clean, Prepare, Compile"> 
     <jar destfile="${dest.dir}/ObservableSort.jar" filesetmanifest="mergewithoutmain"> 
      <manifest> 
       <attribute name="Main-Class" value="cs151.project1.ObservableSortTest"/> 
       <attribute name="Class-Path" value="."/> 
      </manifest> 
      <fileset dir="${home}/bin"/> 
     </jar> 
    </target> 

    <target name="Run" depends="Compile, Build"> 
     <java jar="${dest.dir}/ObservableSort.jar" fork="true" /> 
    </target>   

    <target name="Run with Unit Tests" depends="Compile, Build, Test"> 
     <java jar="${dest.dir}/ObservableSort.jar" fork="true" /> 
    </target> 

    <target name="Javadoc" description="Generate Javadoc" depends="Compile" > 
     <javadoc access="public" author="true" destdir="${dir.javadoc}" nodeprecated="false" nodeprecatedlist="false" noindex="false" nonavbar="false" notree="false" packagenames="cs151.project1.sorters.insertionsort,cs151.project1.sorters.selectionsort,cs151.project1.Quantifiable,cs151.project1,cs151.project1.sorters.quicksort,cs151.project1.views" source="1.6" sourcepath="${src.dir}" splitindex="false" use="true" version="true"/> 
    </target> 

    <target name="Test" depends="Compile"> 
     <junit> 
      <classpath refid="build.class.path" /> 
      <classpath refid="test.class.path" /> 
      <formatter type="plain" usefile="false" /> 
      <test name="${test.suite.class}" haltonerror="true" /> 
     </junit> 
    </target> 
</project> 
+1

请接受[回答](http://meta.stackexchange.com/questions/5234)以前的一些问题。 – oers

回答

0

愚蠢的错误:JUnit的类尚未编译。

解决方案:

  • 工程编译成一个目录X
  • 编译JUnit类到一个目录Ÿ
  • JUnit的JAR添加到目录ž

里面的<测试>标记,将目录X,Y和Z添加到类路径中。

2

您正在使用的测试套件类,而不是路径该类的完全限定名称。

尽管Java类是存储在文件系统文件夹中,Java要求您指定包名称点,而不是削减。

最有可能正确的类名是test._ObservableSortUnitTests也许_ObservableSortUnitTests

+0

我已经尝试了几种不同的classpath和filename的排列方式,我认为它们是正确的,但它似乎并不喜欢我。 – jcampos8782

+0

我有定义$ {home}/test的TestSuite类,它包含在(默认包)中。任何关于下一步尝试的建议? – jcampos8782

+0

如果您的测试位于'$ {home}/test'文件夹中,那么您应该将其添加到Test目标的类路径中。如果TestSuite在默认包中,那么你应该只指定类名'_ObservableSortUnitTests' –

2

如果指定的junit任务测试类的名字,那么你需要使用完全组队参加类名,Mark说。从JUnit task

<junit> 
    <test name="my.test.TestCase"/> 
</junit> 

但是,如果您使用的是batchtest,你可以指定文件名,但你需要添加在年底的.java:

<batchtest fork="yes" todir="${reports.tests}"> 
    <fileset dir="${src.tests}"> 
     <include name="**/*Test*.java"/> 
     <exclude name="**/AllTests.java"/> 
    </fileset> 
    </batchtest> 
+0

它是一个TestSuite类。我有定义$ {home}/test的TestSuite类,它包含在(默认包)中。我尝试了许多不同的可能组合,我认为这些组合是正确的,没有任何工作。任何关于下一步尝试的建议? – jcampos8782

相关问题