2017-03-17 75 views
0

我想在我的junit測試中通過ant任務啓動將codecoverage與jacoco集成在一起。由於我的classpath非常長,叉子崩潰,事實上jacoco迫使我岔開我的junit它給了我一些問題。 我正在使用manifestclasspath將我的類路徑添加到jar文件中,將新的jar作爲參數發送給虛擬機,但它不工作。我的測試運行,但他們都返回一個ClassNotFoundException。 這裏有一個關於我如何配置我的螞蟻進程的portin。使用Manifestclasspath來運行junit的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> 

如果我看一下控制檯日誌運行螞蟻任務,我可以看到我的新.jar文件正在發送時:

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

如果我打開binManifest.jar創建我找到了MANIFEST.MF文件,其所有路徑位於Class-Path屬性中,格式爲:../../Class1/bin ../../Class2/bin ../../ClassN/bin。但由於某些原因,我的所有測試都失敗了,因爲我的類沒有找到。我錯過了什麼?謝謝。

+0

因爲路徑是相對的('../../ Class1/bin'),所以猜測可能是工作目錄不正確。 – Godin

+0

感謝您的回答@Godin。我也這麼想過,但我試過改變我的manifest.mf內容來放絕對路徑或改變我的.jar文件到根文件夾。對於第一種情況,測試會以相同的錯誤崩潰,對於第二種情況,顯示的相對路徑完全相同 –

回答

0

讓我們開始沒有JaCoCo--下面是一個complete and verifiable example,它使用junit fork="true"manifestclasspath

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:除了junit-4.12.jarlib/hamcrest-core-1.3.jar

<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的要求 - 見https://github.com/junit-team/junit4/wiki/Download-and-Install

讓我們通過執行ant確認它沒有例外。

,加入JaCoCo的加入

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

到開始後,

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

周圍junit,最後

<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中的報告。

相關問題