2012-05-18 10 views
5

Mi sono grattato la testa da un po 'di tempo (Googled a bunch, guardato attraverso altri post SO correlati senza alcun risultato). Ho un programma Java composto da due file, Logic e Tests. Tests contiene circa un centinaio di test JUnit e ho ottenuto una percentuale di successo del 100% con detti test chiamando lo javac *.java seguito da java org.junit.runner.JUnitCore Tests. Tuttavia quando eseguo il mio build.xml con una semplice ant -verbose test (per seguire l'uscita visto che sono nuovo a tutto questo), ottengo il seguente output:Nuovo per Ant, ClassNotFoundException con JUnit

[junit] Testsuite: Tests 
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec 
[junit] 
[junit] Null Test: Caused an ERROR 
[junit] Tests 
[junit] java.lang.ClassNotFoundException: Tests 
[junit]  at java.lang.ClassLoader.loadClass(ClassLoader.java:247) 
[junit]  at java.lang.Class.forName0(Native Method) 
[junit]  at java.lang.Class.forName(Class.java:247) 
[junit] 
[junit] 
[junit] Test Tests FAILED 

BUILD SUCCESSFUL 

mio build.xml è la seguente:

<project name="ETL_Automation" default="test" basedir="."> 

<path id="classpath.base"> 
</path> 

<path id="classpath.test"> 
    <pathelement location="${basedir}/mysql-connector-java-5.1.18-bin.jar" /> 
    <pathelement location="${basedir}/junit-4.10.jar"/> 
    <path refid="classpath.base" /> 
</path> 

<target name="compile"> 
    <javac srcdir="${basedir}"> 
    <classpath refid="classpath.test"/> 
    </javac> 
</target> 

<target name="test" depends="compile"> 
    <junit fork="no"> 
    <classpath refid="classpath.test" /> 
    <formatter type="brief" usefile="false" /> 
    <batchtest> 
     <fileset dir="${basedir}/" includes="Tests.class" /> 
    </batchtest> 
    </junit> 
</target> 

<target name="clean" depends="test"> 
    <delete> 
    <fileset dir="${basedir}" includes="*.class"/> 
    </delete> 
</target> 

La struttura di directory è piuttosto semplice. Tests.java, Logic.java, , mysql-connector-java-5.1.18-bin.jar, build.xml e un file di riferimento .properties si trovano tutti nella stessa cartella. Il codice java fa riferimento a file esterni, ma questi non sono correlati a questo particolare problema. Non so se il classpath potrebbe essere la causa di questo problema (dato che sono abbastanza convinto di ciò che attualmente non funziona).

Grazie!

risposta

3

Sarà necessario aggiungere la directory con il Tests.class al classpath.tests classpath (che è ${basedir} nella configurazione)

Prova:

<path id="classpath.test"> 
    <pathelement location="${basedir}/mysql-connector-java-5.1.18-bin.jar" /> 
    <pathelement location="${basedir}/junit-4.10.jar"/> 
    <pathelement location="${basedir}" /> 
    <path refid="classpath.base" /> 
</path> 
Problemi correlati