The simplest JUnit demo program in the world

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.