下面是一個測試程序的例子: public class MyTestCase extends TestCase { /** * call super constrUCtor with a test name * string argument. * @param testName the name of the method that * should be run. */ public MyTestCase(String testName){ super(testName); } /** * called before every test * 在每項測試開始前被調用 */ PRotected void setUp() throws Exception { initSomething(); } /** * called after every test * 在測試完成后被調用 */ protected void tearDown() throws Exception { finalizeSomething(); } /** * this method tests that ... * 這個方法測試…… */ public void testSomeTest1(){ ... } /** * this method tests that ... * 這個方法測試…… */ public void testSomeTest2 (){ ... } }
下面是一個簡單的例子: public class MyTestSuite extends TestSuite { public static Test suite() { TestSuite suite = new TestSuite("Test suite for ..."); // add the first test // 添加第一個測試 MyTestCase mtc = new MyTestCase("testSomeTest1"); suite.addTest(mtc); // add the second test // 添加第一個測試 MyTestCase mtc2 =new MyTestCase("testSomeTest2"); suite.addTest(mtc2); return suite; } }