xzc520

           

          junit in eclipse

          1.介紹一下junit
          ???junit是一個用來單元測試的工具,它可以針對一個/多個類的單個或多個方法進行測試,還可以自動化套件測試.將junit.jar包從www.junit.org.載下來放到eclipse 項目中的java build path中.
          2.創建一個TestCase
          ???File > New > JUnit Test Case 或者點擊"newTestCase.gif"來創建一個TestCase

          ???代碼如下
          ???import junit.framework.TestCase;
          ???public class SampleTest extends TestCase {
          ???? private java.util.List emptyList;
          ???? /**
          ????? * Sets up the test fixture.
          ????? * (Called before every test case method.)
          ????? */
          ???? protected void setUp() {
          ????????? emptyList = new java.util.ArrayList();
          ???? }?
          ???? /**
          ????? * Tears down the test fixture.
          ????? * (Called after every test case method.)
          ????? */
          ???? protected void tearDown() {
          ????????? emptyList = null;
          ???? }?
          ???? public void testSomeBehavior() {
          ????????? assertEquals("Empty list should have 0 elements", 0, emptyList.size());
          ???? }?
          ???? public void testForException() {
          ????????? try {
          ?????????????? Object o = emptyList.get(0);
          ?????????????? fail("Should raise an IndexOutOfBoundsException");
          ????????? }
          ????????? catch (IndexOutOfBoundsException success) {
          ????????? }
          ???? }
          }
          這個例子有兩個方法需要測試,第一個方法測試list中沒有任何對象,第二個方法測試沒有使用斷言,它一定會成功

          3.創建一個TestSuite
          ???通過測試套件可以運行多個測試用例
          ?? (3.1)選擇???File > New > Other... > Java > JUnit > JUnit Test Suite. 或者newTestCase.gifOther... > Java > JUnit > JUnit Test Suite,
          import junit.framework.Test;
          import junit.framework.TestSuite;

          ???public class AllTests {

          ????public static Test suite() {
          ????????TestSuite suite = new TestSuite("Test for com.xu.Test");
          ????????//$JUnit-BEGIN$
          ????????suite.addTestSuite(SampleTest .class);
          ????????//$JUnit-END$
          ????????return suite;
          ????}

          }

          posted on 2006-10-17 15:56 嫁蛙 閱讀(206) 評論(0)  編輯  收藏 所屬分類: open source tools


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 孟津县| 揭阳市| 靖州| 平定县| 怀仁县| 昆山市| 泾源县| 滁州市| 安顺市| 杭锦旗| 威信县| 尉犁县| 金山区| 当雄县| 天津市| 长岭县| 滨海县| 株洲县| 松滋市| 陆川县| 邓州市| 焦作市| 桓台县| 乌拉特后旗| 象州县| 兴山县| 峡江县| 海晏县| 宜州市| 德惠市| 越西县| 望谟县| 云和县| 平塘县| 如皋市| 云梦县| 江津市| 青海省| 平乡县| 巴彦县| 沂源县|