一. 使用Junit进行測试
1. Java业务代码:
public class HelloWorld { // 測试返回"world" public String hello() { return "world"; } // 測试返回"hello" public String world() { return "hello"; } // 測试为空 public String nil() { return null; } // 測试不为空 public String notNil() { return "abc"; } // 測试抛出异常 public String ext() { return null; } }
2. 使用junit3測试, 继承TestCase
import junit.framework.TestCase; public class HelloWorldTest extends TestCase { private HelloWorld helloWorld; @Override protected void setUp() throws Exception { helloWorld = new HelloWorld(); System.out.println("helloWorld init"); } public void testHello() { String str = helloWorld.hello(); assertEquals("測试world失败", str, "world"); } public void testWorld() { String str = helloWorld.world(); assertEquals("測试world失败", str, "hello"); } public void testNotNil() { assertNotNull("对象为空", helloWorld.notNil()); } public void testNil() { assertNull("对象不为空", helloWorld.nil()); } public void testExt() { try { helloWorld.ext(); fail("没有抛出异常"); } catch (NumberFormatException e) { } } @Override protected void tearDown() throws Exception { System.out.println("hello world destory"); helloWorld = null; } }
3. 使用junit4測试, 使用注解
import org.junit.After; import org.junit.Before; import org.junit.Test; import static org.junit.Assert.*; // 静态引入, assertEquals兼容junit3 public class HelloWorldTest { private HelloWorld helloWorld; @Before public void setUp() { helloWorld = new HelloWorld(); } @Test public void testHello() { String str = helloWorld.hello(); assertEquals("hello測试失败",str,"world"); } @Test public void testWorld() { String str = helloWorld.world(); assertEquals("world測试失败",str, "hello"); } @Test public void testNil() { assertNull("对象不为空",helloWorld.nil()); } @Test public void testNotNil() { assertNotNull("对象为空", helloWorld.notNil()); } @Test(expected=NumberFormatException.class) public void testExt() { helloWorld.ext(); } @After public void tearDown() { helloWorld = null; } }
二. ant整合junit測试
1. 生成測试报告:
<?xml version="1.0" encoding="UTF-8"?> <project name="junit-test"> <property name="src" location="src" /> <property name="test" location="test" /> <property name="lib" location="lib" /> <property name="build" location="build" /> <property name="build.classes" location="${build}/classes" /> <property name="build.test" location="${build}/test" /> <property name="build.test.classes" location="${build.test}/classes" /> <property name="build.test.report" location="${build.test}/report" /> <!-- <property name="runTest" value="com.zdp.test.HelloWorldTest" /> --> <property name="runTest" value="**/*Test.class"></property> <!-- 手动指明classpath, 一个path中能够加多个classpath,使用链式编写 --> <path id="lib_path"> <fileset dir="${lib}" includes="*.jar" /> </path> <path id="compile_path"> <path refid="lib_path" /> <pathelement location="${build.classes}"/> </path> <path id="compile_test_path"> <path refid="compile_path" /> <pathelement location="${build.test.classes}"/> </path> <target name="clean"> <echo>1.開始清理文件</echo> <delete dir="${build}"></delete> </target> <target name="init"> <echo>2.项目的初始化</echo> <mkdir dir="${build}"/> <mkdir dir="${build.classes}"/> <mkdir dir="${build.test}"/> <mkdir dir="${build.test.classes}"/> <mkdir dir="${build.test.report}"/> </target> <target name="compile_src" depends="init"> <echo>3.编译源文件</echo> <javac srcdir="${src}" destdir="${build.classes}" classpathref="lib_path" includeantruntime="true"/> </target> <target name="compile_test" depends="compile_src"> <echo>4.编译測试文件</echo> <javac srcdir="${test}" destdir="${build.test.classes}" classpathref="compile_path" includeantruntime="true"/> </target> <target name="run_test" depends="clean, compile_test"> <echo>5.执行单元測试</echo> <junit printsummary="false" haltonfailure="false"> <classpath refid="compile_test_path"></classpath> <formatter type="xml"/> <batchtest todir="${build.test.report}"> <fileset dir="${build.test.classes}" includes="${runTest}"/> </batchtest> </junit> <junitreport todir="${build.test.report}"> <fileset dir="${build.test.report}" includes="TEST-*.xml"/> <report format="frames" todir="${build.test.report}/html"/> </junitreport> </target> <target name="end" depends="run_test"> <echo>6.測试结束</echo> </target> </project>
2. 生成doc文档并打包
<?xml version="1.0" encoding="UTF-8"?
> <project default="zip"> <property name="src" location="src"></property> <property name="build" location="build"></property> <property name="build.classes" location="${build}/classes"></property> <property name="build.doc" location="${build}/doc/api"></property> <property name="zip" location="${build}/zip"></property> <property name="version" value="SNAPSHOT_0.1"></property> <property name="projectName" value="helloworld_${version}"></property> <property name="zipFileName" value="helloworld_${version}.zip"></property> <target name="clean"> <echo>1.開始清理文件</echo> <delete dir="${build}"></delete> </target> <target name="init"> <echo>2.项目的初始化</echo> <mkdir dir="${build}"/> <mkdir dir="${build.classes}"/> <mkdir dir="${build.doc}"/> </target> <target name="doc" depends="init"> <echo>3.生成doc文档</echo> <javadoc sourcepath="${src}" private="true" windowtitle="HelloWorld JAVA DOC" use="true" packagenames="com.zdp.*" destdir="${build.doc}" charset="UTF-8" docencoding="UTF-8" encoding="UTF-8"> <classpath path="${build.classes}"></classpath> </javadoc> </target> <target name="zip" depends="doc"> <echo>4.生成zip包</echo> <zip destfile="${zip}/${zipFileName}" duplicate="preserve"> <zipfileset dir="${build.doc}" includes="**/*.*" prefix="${projectName}/doc/api"></zipfileset> <zipfileset dir="${src}" includes="**/*.*" prefix="${projectName}/src"></zipfileset> </zip> </target> </project>