First, we will not talk about the packages required in JUnit.
Write classes:
Package untitled9;
Import java. util .*;
Public class sayhello {
Public sayhello (){
}
// Test the Function
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
// Multiple test functions
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
}
/*************************************** *******/
// The following are the test classes:
Package untitled9;
Import JUnit. Framework .*;
Public class testsayhello
Extends testcase {
Private sayhello = NULL;
Protected void setup () throws exception {
Super. Setup ();
Sayhello = new sayhello ();
}
Protected void teardown () throws exception {
Sayhello = NULL;
Super. teardown ();
}
// Single test function
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 );
}
}
// Compile and run it. It's easy.