JUNIT容易实例及模板

JUNIT简单实例及模板
package com.pea.junit;

import java.util.Hashtable;
import junit.framework.Assert;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class JunitB extends TestCase {

	/** 定义你需要测试的类及用到的变量 */
	public Hashtable hasha = null;//
	public Hashtable hashb = null;

	/** **************************************************** */
	public JunitB(String name) {
	   super(name); // 创建子类
	}

	/** 用setUp进行初始化操作 */
	protected void setUp() throws Exception {
	   super.setUp();
	   hasha = new Hashtable(); // 这里
	}

	/** 用tearDown来销毁所占用的资源 */
	protected void tearDown() throws Exception {
	   super.tearDown();
	  // System.gc();
	}

	/** 写一个测试方法断言期望的结果 */
	public void testBodyStatus() {
	   // hasha =new Hashtable(); // 有此句后也可去掉setUp() tearDown()
	   assertNotNull(hasha);
	   // hasha.put("0","let's try again"); // test1.error版
	   assertTrue(hasha.isEmpty()); // 期望为空
	}

	/** 再写一个测试方法断言期望的结果 */
	public void testBodySame() {
	   hashb=(Hashtable)hasha.clone(); //test2.error版
	  //hashb = hasha; // test2.OK 版
	   Assert.assertSame(hasha, hashb);
	}

	/** suite()方法,使用反射动态的创建一个包含所有的testXxxx方法的测试套件 */
	public static TestSuite suite() {
	   return new TestSuite(JunitB.class);
	}

	/** 写一个main()运行测试 */
	public static void main(String args[]) {
	   junit.textui.TestRunner.run(suite()); // 以文本运行器的方式方便的
	   //junit.swingui.TestRunner.run(JunitB.class);
	}
}