世界上最简单的 junit demo 程序

首先下在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);
  }
}

//编译运行就可以了,简单吧。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值