世界上最簡單的 junit demo 程式

來源:互聯網
上載者:User

首先下在junit所需要的包,這裡就不多說了。

寫個類:

package untitled9;

import java.util.*;

public class SayHello {
  public SayHello() {
  }
  //測試函數
  public String[] split1(String str, String delim) {
    StringTokenizer st = new StringTokenizer(str, delim);
    int length = st.countTokens();
    String[] result = new String[length];
    for (int i = 0; i < length; i++) {
      result[i] = st.nextToken();
    }
    return result;
  }//end split1
  //多項測試函數
  public String[] split2(String str, String delim) {
    final int delimLength = delim.length();
    int pos = 0;
    int index = 0;
    ArrayList list = new ArrayList();

    while((index = str.indexOf(delim,pos)) != 1){
      list.add(str.substring(pos,index));
      pos = index + delimLength;
    }
    list.add(str.substring(pos));
    return ((String[])list.toArray((new String[0])));
  }//end split1
}

/**********************************************/
//以下是測試類別:

package untitled9;

import junit.framework.*;

public class TestSayHello
    extends TestCase {
  private SayHello sayHello = null;

  protected void setUp() throws Exception {
    super.setUp();
    sayHello = new SayHello();
  }

  protected void tearDown() throws Exception {
    sayHello = null;
    super.tearDown();
  }

  //單項測試函數
  public void testSplit() {
    SayHello shello = new SayHello();
    String str = "a,b,c";
    String delim = ",";

    String[] actualReturn = shello.split1(str, delim);

    /**@todo fill in the test code*/
    assertNotNull(actualReturn);
    assertEquals(3, actualReturn.length);
    assertEquals("a", actualReturn[0]);
    assertEquals("b", actualReturn[1]);
    assertEquals("c", actualReturn[2]);
  }
  public static void main(String[] args) {
    junit.swingui.TestRunner.run(TestSayHello.class);
  }
}

//編譯運行就可以了,簡單吧。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.