接上文,本文主要介绍Junit+ant+JaCoCo集成使用
1、工具的下载上传
需要的工具有:
apache-ant-1.10.5-bin.tar
apache-tomcat-8.0.50.tar
jacoco-0.8.4-20190222.010339-7.zip
下载jacoco上传到到具体服务器:
也可以到官网下载:https://www.eclemma.org/jacoco/
下载tomcat上传到服务器:https://tomcat.apache.org/
下载ant上传到服务器:http://ant.apache.org/bindownload.cgi
上传到具体的服务器,目录如下:
2、解压配置环境变量
tar -xvf apache-tomcat-8.0.50.tar.gz tar -xvf apache-tomcat-8.0.50.tar unzip jacoco-0.8.4-20190222.010339-7.zip
2.1、设置用户环境变量:输入命令:vim ~/.bashrc,有的环境是vim ~/.bash_profile,打开文件,输入ant需要的环境变量;如下内容:
ANT_HOME=/home/antjacocolinux/apache-ant-1.10.5 PATH=$ANT_HOME/bin:$PATH export PATH ANT_HOME
2.2 退出并保存:esc => shift + : => wq => 回车,设置环境变量后必须使命令生效!!再输入命令使之生效:source ~/.bash_profile
修改apache-tomcat-8.0.50目录下bin文件夹下的catalina.sh的JAVA_OPTS配置,如图新增:
JAVA_OPTS="$JAVA_OPTS -javaagent:/home/antjacocolinux/jacoco-0.8.4/lib/jacocoagent.jar=includes=*,output=tcpserver,append=true,port=6101,address=*"
3、准备好ant项目,修改build.xml配置:
创建一个java工程,编写Calculator类
public class Calculator { public int evaluate(String expression) { int sum = 0; for (String summand: expression.split("\+")) sum += Integer.valueOf(summand); return sum; } public int missAdd(int a, int b) { return a + b; } public boolean beyondZero(int v) { if (v > 0) { return true; } else { return false; } } }
创建单元测试CalculatorTest 类
import static org.junit.Assert.assertEquals; import org.junit.Test; public class CalculatorTest { @Test public void evaluatesExpression() { Calculator calculator = new Calculator(); int sum = calculator.evaluate("1+2+3"); assertEquals(6, sum); } @Test public void testChoose() { Calculator calculator = new Calculator(); assertEquals(true, calculator.beyondZero(19)); } }
修改build.xml文件
<?xml version="1.0" encoding="UTF-8"?> <project name="junit-example" default="rebuild" xmlns:jacoco="antlib:org.jacoco.ant"> <description> 这是一个简单的Demo,用来演示junit+ant+Jacoco </description> <property name="main.build.dir" value="build/main"/> <property name="main.src.dir" value="src/main/java"/> <property name="test.build.dir" value="build/test"/> <property name="test.src.dir" value="src/test/java"/> <property name="result.dir" location="./target" /> <property name="result.report.dir" location="${result.dir}/site/jacoco" /> <property name="result.exec.file" location="${result.dir}/jacoco.exec" /> <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml"> <classpath path="lib/jacocoant.jar" /> </taskdef> <target name="clean"> <delete dir="${result.dir}" /> <delete dir="build" /> </target> <path id="classpath.test"> <pathelement location="lib/junit-4.12.jar"/> <pathelement location="lib/hamcrest-core-1.3.jar"/> <pathelement location="${main.build.dir}"/> </path> <target name="compile"> <mkdir dir="${main.build.dir}"/> <javac srcdir="${main.src.dir}" destdir="${main.build.dir}" debug="true" includeantruntime="false"/> </target> <target name="test-compile" depends="compile"> <mkdir dir="${test.build.dir}"/> <javac srcdir="${test.src.dir}" destdir="${test.build.dir}" includeantruntime="false"> <classpath refid="classpath.test"/> </javac> </target> <target name="test" depends="test-compile"> <jacoco:coverage destfile="${result.exec.file}"> <junit printsummary="yes" haltonfailure="yes" fork="true" forkmode="once"> <classpath> <path refid="classpath.test"/> <pathelement location="${test.build.dir}"/> </classpath> <formatter type="brief" usefile="false" /> <batchtest> <fileset dir="${test.src.dir}" includes="**/*Test.java" /> </batchtest> </junit> </jacoco:coverage> </target> <target name="report" depends="test"> <jacoco:report> <executiondata> <file file="${result.exec.file}" /> </executiondata> <structure name="JaCoCo Ant Example"> <classfiles> <fileset dir="${main.build.dir}" /> </classfiles> <sourcefiles encoding="UTF-8"> <fileset dir="${main.src.dir}" /> </sourcefiles> </structure> <html destdir="${result.report.dir}" /> <csv destfile="${result.report.dir}/report.csv" /> <xml destfile="${result.report.dir}/report.xml" /> </jacoco:report> </target>
<target name="rebuild" depends="clean,report" /> </project>
将该项目放到apache-tomcat-8.0.50/webapps下,到输入命令:
ant report
在对应的目录下会生成html的报告,目录如下:
点击可以查看具体的报告:
相关代码下载:代码下载