2017-03-17 112 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中的报告。

相关问题