I. Introduction JUnit: a Java testing framework for source code development. It is used to write and run repeated tests. It is an example of xunit, a unit testing framework system (for Java ). Below are some features of JUnit: 1. The provided API allows you to write reusable unit test cases with clear test results 2. Three methods are provided to display your test results. 3. Provides the unit test case batch operation function 4. Ultra-lightweight and easy to use, without commercial spoofing and useless wizard 5. The entire framework is well designed and easy to expand Ii. Download Http://www.junit.org can download to the latest version of JUnit Iii. Instances 1. Write test objectives (business-related classes) /**
* * Test object * Author: zhuyongke * Date: 2011-8-13 07:20:58 */ Public class calcuator {
Public int add (int N1, int N2 ){ Return N1 + N2; } } Ii. Write Test Cases Import JUnit. Framework. testcase; /** * Test Cases * File: testcalcuator. Java * Author: zhuyongke * Date: 2011-38-13 07:25:00 */ Public class testcalcuator Extends testcase { Public void testadd (){
Calcuator = new calcuator (); Int result = calcuator. Add (1, 2 ); Assertequals (3, result ); } } Iii. Write Test Unit Import JUnit. Framework. testsuite;
Import JUnit. Framework. test; Import JUnit. textui. testrunner; /** * Test Unit * File: testall. Java * Auth: zhuyongke * Date: 2011-38-13 07:28:36 */ Public class testall Extends testsuite { Public Static Test Suite (){
Testsuite suite = new testsuite ("testsuite test ");
Suite. addtestsuite (testcalcuator. Class ); Return suite; } Public static Void main (string ARGs []) { Testrunner. Run (Suite ()); } } 4. Main () method for running unit test The console prints the following results: Time: 0 OK (1 test) This is just a simple test example. Further research is required! Haha! |