AntDemo.java
View Code
1 package com.xiong.ant.demo; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 public class AntDemo 7 { 8 public boolean save() 9 { 10 return true; 11 } 12 13 public boolean delete() 14 { 15 return false; 16 } 17 18 public int update() 19 { 20 return 1; 21 } 22 23 public List<Object> findAll() 24 { 25 return new ArrayList<Object>(); 26 } 27 28 public Object findById(int id) 29 { 30 return "Object"; 31 } 32 33 public Object checkNonNull() 34 { 35 return "123"; 36 } 37 38 public Object checkNull() 39 { 40 return null; 41 } 42 43 public void checkException() 44 { 45 throw new RuntimeException("运行期异常"); 46 } 47 }
ADemoTest.java
View Code
1 package com.xiong.ant.demo.test; 2 3 import static org.junit.Assert.*; 4 5 import org.junit.After; 6 import org.junit.Before; 7 import org.junit.Test; 8 9 import com.xiong.ant.demo.AntDemo; 10 11 public class ADemoTest 12 { 13 private AntDemo ad = null; 14 15 @Before 16 public void setUp() 17 { 18 this.ad = new AntDemo(); 19 } 20 21 @Test 22 public void testCheckNonNull() 23 { 24 assertNotNull("结果为null", this.ad.checkNonNull()); 25 } 26 27 @Test 28 public void testCheckNull() 29 { 30 assertNull("结果不为null", this.ad.checkNull()); 31 } 32 33 @Test(expected = RuntimeException.class) 34 public void testCheckException() 35 { 36 this.ad.checkException(); 37 } 38 39 @After 40 public void tearDown() 41 { 42 this.ad = null; 43 } 44 }
AntDemoTest.java
View Code
1 package com.xiong.ant.demo.test; 2 3 import static org.junit.Assert.*; 4 5 import org.junit.After; 6 import org.junit.Before; 7 import org.junit.Test; 8 9 import com.xiong.ant.demo.AntDemo; 10 11 public class AntDemoTest 12 { 13 private AntDemo ad = null; 14 15 @Before 16 public void setUp() 17 { 18 this.ad = new AntDemo(); 19 } 20 21 @Test 22 public void testSave() 23 { 24 assertTrue("插入数据失败!", this.ad.save()); 25 } 26 27 @Test 28 public void testDelete() 29 { 30 assertFalse("删除数据成功!", this.ad.delete()); 31 } 32 33 @Test 34 public void testUpdate() 35 { 36 assertEquals("更新数据失败!", 1, this.ad.update()); 37 } 38 39 @Test 40 public void testFindAll() 41 { 42 assertNotNull("查询结果为null", this.ad.findAll()); 43 } 44 45 @Test 46 public void testFindById() 47 { 48 assertNotNull("查询结果为null", this.ad.findById(10)); 49 } 50 51 @After 52 public void tearDown() 53 { 54 this.ad = null; 55 } 56 }
build.xml
View Code
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project> 3 <echo>设置属性</echo> 4 <property file="build.properties"></property> 5 <property name="build.classes" location="${build.dir}/classes"></property> 6 <property name="build.src" location="${build.dir}/src"></property> 7 <property name="build.dist" location="${build.dir}/dist"></property> 8 <property name="build.test.dir" location="${build.dir}/test"></property> 9 <property name="build.test.classes" location="${build.test.dir}/classes"></property> 10 <property name="build.test.report" location="${build.test.dir}/report"></property> 11 12 <echo>设置环境属性</echo> 13 <path id="JunitLib"> 14 <fileset dir="${junit.jar}" includes="*.jar"></fileset> 15 </path> 16 17 <path id="JunitCompileContext"> 18 <path refid="JunitLib"></path> 19 <pathelement location="${build.classes}"/> 20 </path> 21 22 <path id="JunitExecuteContext"> 23 <path refid="JunitCompileContext"></path> 24 <pathelement location="${build.test.classes}"/> 25 </path> 26 27 28 <echo>设置文件集</echo> 29 <fileset id="WaitForCompile" dir="${src.dir}" includes="**/*.*"></fileset> 30 31 32 <echo>初始化</echo> 33 <target name="init"> 34 <delete dir="${build.dir}"></delete> 35 <mkdir dir="${build.dir}"/> 36 <mkdir dir="${build.classes}"/> 37 <mkdir dir="${build.src}"/> 38 <mkdir dir="${build.dist}"/> 39 <mkdir dir="${build.test.dir}"/> 40 <mkdir dir="${build.test.classes}"/> 41 <mkdir dir="${build.test.report}"/> 42 </target> 43 44 <echo>复制源文件</echo> 45 <target name="copySrc"> 46 <copy todir="${build.src}" failonerror="true"> 47 <fileset refid="WaitForCompile"></fileset> 48 </copy> 49 </target> 50 51 <echo>编译源文件</echo> 52 <target name="compile" depends="init"> 53 <javac classpathref="JunitCompileContext" failonerror="true" srcdir="${src.dir}" destdir="${build.classes}" includeantruntime="true"></javac> 54 </target> 55 56 <echo>编译Junit测试文件</echo> 57 <target name="CompileJunitTest" depends="compile,copySrc"> 58 <javac classpathref="JunitCompileContext" failonerror="true" srcdir="${test.src.dir}" destdir="${build.test.classes}" includeantruntime="true"></javac> 59 </target> 60 61 <echo>自动运行单元测试并生成单元测试报告</echo> 62 <target name="AutoExcuteJunit" depends="CompileJunitTest"> 63 <junit haltonfailure="true" printsummary="true"> 64 <classpath refid="JunitExecuteContext"></classpath> 65 <formatter type="xml" usefile="true"/> 66 <batchtest todir="${build.test.report}"> 67 <fileset dir="${build.test.classes}" includes="${test.classname}"></fileset> 68 </batchtest> 69 </junit> 70 <junitreport todir="${build.test.report}"> 71 <fileset dir="${build.test.report}" includes="TEST-*.xml"></fileset> 72 <report format="frames" todir="${build.test.report}/html"/> 73 </junitreport> 74 </target> 75 76 <echo>生成Jar</echo> 77 <target name="jar" depends="AutoExcuteJunit"> 78 <jar destfile="${build.dist}/${jar.name}" basedir="${build.classes}"></jar> 79 </target> 80 81 </project>
build.properties
View Code
1 src.dir=src 2 test.src.dir=test 3 build.dir=Build 4 jar.name=AntJuintDemo.jar 5 execute.class=com.xion.ant.demo.MyFirstAntDemo 6 7 junit.jar=lib 8 test.classname=**/*Test.class
生成结果: