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 嫁蛙 閱讀(205) 評論(0)  編輯  收藏 所屬分類: open source tools


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


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 深泽县| 博兴县| 成武县| 阿图什市| 闻喜县| 辛集市| 老河口市| 隆化县| 梁河县| 疏勒县| 固安县| 普陀区| 文登市| 武汉市| 永州市| 田阳县| 比如县| 阳山县| 天门市| 桦川县| 静乐县| 沙雅县| 桑植县| 紫金县| 马鞍山市| 台东市| 祁阳县| 丹东市| 温宿县| 阜阳市| 兴海县| 大洼县| 蓬安县| 徐水县| 竹溪县| 治县。| 大竹县| 左贡县| 元谋县| 陕西省| 通州区|