2013年5月13日 星期一

[Unit Testing]Ant with JoCoCo

近排的工作是在準備做Websphere 8.5 + Hibernate 4.2 Upgrade.


我需要寫不同的JUnit Test Case 去 Test 不同的DAO 和 Persistence Unit.


公司的要求相當低:

1. 不講究Code Coverage.
2.  有部份同事為左方便,從來沒有Test 過其他 Create , Retrieve , and Update Function.( Code Coverage below 10%)
3. 用 DB 現成Data, 因為Data 會在很多時候變動,使 Unit Test Rely on DB Data. 不夠獨立性。


我本着 Tech Geek 的心態,為左嘗試不同的小技術,就把他們的 ANT Script 進行小修改:
1. 首先,在 build.xml 修改,加入 xmlns:jacoco="antlib:org.jacoco.ant" 。

<project name="Impl" default="default" basedir="." xmlns:jacoco="antlib:org.jacoco.ant">
2.  加入 JaCoCo 的 Task Def
<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
        <classpath path="lib/jacocoant.jar" />
    </taskdef>

3. 把 需要行 Code Coverage 的 Java Task 用 <jacoco:coverage></jacoco:coverage> 包上。

4. 再將 已 Compile 好 的.class path 和 src file path 在紅的地方填上。

   <target name="test-suite" depends="test-compile" description="Execute unit tests suite">
             <echo> Test classpath ${proj.test.classpath}</echo>
           
            <jacoco:coverage>
            <java fork="yes" classname="org.junit.runner.JUnitCore">
                  <classpath>
                      <path refid="proj.test.classpath"/>
                  </classpath>
                 
  <arg line="com.xzxxx.abcd.ordermanagement.persistence.impl.test.TestTradeHistoryHibernatePersistence" />
        </java>
        </jacoco:coverage>
            <jacoco:report>
                          
        <executiondata>
            <file file="jacoco.exec"/>
        </executiondata>
                          
        <structure name="Example Project">
            <classfiles>
                <fileset dir="build\classes"/>
            </classfiles>
            <sourcefiles encoding="UTF-8">
                <fileset dir="src\java"/>
            </sourcefiles>
        </structure>
                          
        <html destdir="report"/>
                          
    </jacoco:report>
    </target>

5. 最後,便是 行 ant <你的 target-name>

6. Report 會在 Report Directory Generate 出來。

HK Coding Style below.



Ant:  http://ant.apache.org/
JaCoCo: http://www.eclemma.org/jacoco/

沒有留言:

張貼留言