2017-03-17 15 views
0

antタスクによって起動されたjunitテストでcodecoverageとjacocoを統合しようとしています。私のクラスパスがかなり長くフォークがクラ​​ッシュするので、ジャココが私にジュニットをフォークさせるという事実は、私にいくつかの問題を与えています。 私はクラスパスをjarファイルに追加するためにmanifestclasspathを使用しています。新しいJarをVMへの引数として送信しますが、動作していません。テストは実行されますが、すべてClassNotFoundExceptionが返されます。 ここでは、私のantプロセスをどのように設定したのかを示します。JUNITを実行するためにManifestクラスパスを使用しているAnt ClassNotFoundException

<path refid="bin.classpath"/> 

bin.classpathには、.jarファイルに入れる必要があるすべてのパスが含まれています。

<target name="run-unit-tests" depends="init" description="Runs all the unit tests"> 
    <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml"> 
    <classpath path="jacocoant.jar" /> 
    </taskdef> 

    <manifestclasspath property="binjar" jarfile="binManifest.jar"> 
    <classpath refid="bin.classpath" /> 
    </manifestclasspath> 

    <jar destfile="manifestJars/binManifest.jar"> 
    <manifest> 
     <attribute name="Class-Path" value="${binjar}" /> 
    </manifest> 
    </jar> 

    <jacoco:coverage destfile="results/jacoco.exec" xmlns:jacoco="antlib:org.jacoco.ant"> 
    <junit fork="true" reloading="false" showoutput="true"> 
     <classpath> 
     <pathelement path="${projects.dir}/AntProject/manifestJars/binManifest.jar" /> 
     </classpath> 

     <batchtest todir="${test.data.dir}" fork="true"> 
     <fileset dir="${projects.dir}/ProjectFolder1/src" /> 
     <fileset dir="${projects.dir}/ProjectFolder2/src" /> 
     </batchtest> 
    </junit> 
    </jacoco:coverage> 
</target> 

のantタスクを実行しているときに私はコンソールログを見れば、私は私の新しい.jarファイルが送信されているかを確認することができます

-classpath''C:\Users\XXX\Project\AntProject\manifestJars\binManifest.jar; 

を私はbinManifest.jarは私が見つけ作成開いた場合MANIFEST.MFファイルは、クラスパスプロパティ内のすべてのパスを../../Class1/bin ../../Class2/bin ../../ClassN/binの形式で格納します。しかし何らかの理由で私のクラスが見つからないため、すべてのテストが失敗します。私は何が欠けていますか?ありがとう。

+0

パスは相対パス( '../../ Class1/bin')なので、作業ディレクトリが正しくない可能性があります。 – Godin

+0

答えてくれてありがとう@Godin。私もそう思っていますが、絶対パスを入れたり、.jarファイルをルートフォルダに変更するためにmanifest.mfの内容を変更しようとしました。最初のケースではテストは同じエラーでクラッシュし、2番目のケースでは表示される相対パスはまったく同じでした –

答えて

0

JaCoCoなしで始めましょう - とmanifestclasspathを使用するcomplete and verifiable exampleは以下のとおりです。

main/Example.java

class Example { 
    public static void sayHello() { 
    System.out.println(); 
    } 
} 

test/ExampleTest.java

public class ExampleTest { 
    @org.junit.Test 
    public void test() { 
    Example.sayHello(); 
    } 
} 

build.xml

<project xmlns:jacoco="antlib:org.jacoco.ant" default="build"> 
    <target name="build"> 
    <delete dir="bin" /> 
    <mkdir dir="bin/main-classes" /> 
    <mkdir dir="bin/test-classes" /> 

    <javac target="1.5" debug="true" destdir="bin/main-classes"> 
     <src path="main" /> 
    </javac> 

    <javac target="1.5" debug="true" destdir="bin/test-classes"> 
     <src path="test" /> 
     <classpath> 
     <pathelement path="bin/main-classes"/> 
     <pathelement path="lib/junit-4.12.jar"/> 
     <!-- otherwise "java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing" --> 
     <pathelement path="lib/hamcrest-core-1.3.jar"/> 
     </classpath> 
    </javac> 

    <manifestclasspath property="binjar" jarfile="bin/manifest.jar"> 
     <classpath> 
     <pathelement path="bin/main-classes"/> 
     <pathelement path="bin/test-classes"/> 

     <pathelement path="lib/junit-4.12.jar"/> 
     <!-- otherwise "java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing" --> 
     <pathelement path="lib/hamcrest-core-1.3.jar"/> 
     </classpath> 
    </manifestclasspath> 

    <jar destfile="bin/manifest.jar"> 
     <manifest> 
     <attribute name="Class-Path" value="${binjar}" /> 
     </manifest> 
    </jar> 

    <junit fork="true" showoutput="true"> 
     <formatter type="brief" usefile="false" /> 
     <classpath> 
     <pathelement path="bin/manifest.jar" /> 
     </classpath> 
     <batchtest> 
     <fileset dir="test" includes="**/*Test.java" /> 
     </batchtest> 
    </junit> 
    </target> 
</project> 

junit-4.12.jarに加えlib/hamcrest-core-1.3.jarの注意存在JUnitのの要件に従って -参照

antを実行することで例外なく動作することを確認しましょう。最後まで先頭に

<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml"> 
    <classpath path="jacocoant.jar" /> 
</taskdef> 

の添加によるJaCoCoの添加後

junit周り
<jacoco:coverage destfile="bin/jacoco.exec" xmlns:jacoco="antlib:org.jacoco.ant"> 
... 
</jacoco:coverage> 

し、最終的に

<jacoco:report> 
    <executiondata> 
    <file file="bin/jacoco.exec"/> 
    </executiondata> 
    <structure name="JaCoCo Ant Example"> 
    <classfiles> 
     <fileset dir="bin/main-classes"/> 
    </classfiles> 
    <sourcefiles encoding="UTF-8"> 
     <fileset dir="main"/> 
    </sourcefiles> 
    </structure> 
    <html destdir="bin/report"/> 
</jacoco:report> 

は大したことではありません。 antを実行すると、ディレクトリbin/reportにレポートが作成されます。

関連する問題