首先下在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);
}
}
//编译运行就可以了,简单吧。