Getting started with JUnit

Source: Internet
Author: User

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!

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.