Unit Testing of Java programs-entering the paradise of JUnit

Source: Internet
Author: User

Author: keld H. Hansen Translation: cmanlh original address

I. Introduction
2. Enter JUnit's paradise
Iii. Round class
4. Analyze and obtain test cases

JUnit Introduction
JUnit is an open-source project using Java as the development language. It provides a great architecture for unit testing. With JUnit, the Code mentioned in the previous section will become:

Assertequals (0, C. paruptohole (0 ));
Assertequals (8, C. paruptohole (2 ));
Assertequals (72, C. paruptohole (18 ));

Is such a beautiful code more in line with our wishes. At the same time, JUnit also provides a better error reporting mechanism. For example, if you change "72" in the last line to "71", JUnit will provide the following message:

There was 1 Failure:
1) testsomething (Hansen. playground. testcourse)
JUnit. Framework. assertionfailederror:
Expected: <71> but was: <72>

At this moment, we automatically obtain the actual value of the expression "C. paruptohole (18.

JUnit provides many functions, but in the following example, only the most common and simple ones are shown. If you are interested in exploring JUnit and playing its powerful role, log on to www.junit.org and have some useful articles.

In order for JUnit to play a role, we must abide by some rules or rules. For now:

1. The test class you designed must inherit the testcase class in the JUnit class.

2. If your test case has some common data or operations, perform these operations in the setup method.

3. The test code (for example, calling "assertequals") must be put in one or more methods whose names start with "test ".

In this way, the above three tests will become the following classes:

Package Nansen. playground;

Import JUnit. Framework .*;

Public class testcourse extends testcase ...{
Private course C;

Public testcourse (string name )...{
Super (name );
}

Protected void setup ()...{
C = New Course ();
C. setname ("st. Andrews ");
Int [] par =... {4, 4, 4, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4 };
C. setpar (PAR );
}

Public void testsomething ()...{
Assertequals (0, C. paruptohole (0 ));
Assertequals (8, C. paruptohole (2 ));
Assertequals (72, C. paruptohole (18 ));
}
}

Note: We have imported the JUnit. Framework Package, which can be downloaded from www.junit.org. The setup method has a sibling method: teardown, which is used to release the resources allocated by the setup method.

Tools for running JUnit Testing
JUnit provides a running tool for its testing program, which is also written in Java. It can be run in the command line through batch processing, for example, Java JUnit. textui. testrunner Hansen. playground. testcourse

Alternatively, you can call the AWT or swing visual interface:

Java JUnit. awtui. testrunner Hansen. playground. testcourse

-- Or --

Java JUnit. swingui. testrunner Hansen. playground. testcourse

They will create the corresponding Gui:

  

Enter the class name and press "run" to test the program.

For automated testing, we prefer batch processing. In this case, it can be implemented directly in the test program itself. You need to add a main method:

Public static void main (string [] ARGs )...{
JUnit. textui. testruuner. Run (testcourse. Class );
}

Assert-Method
In fact, there are many methods similar to "assertequals" in the above Code, some of which are important as follows:

Method Name Function
Assertequals (A, B) Assertion two parameters are equal. Where a and B must be the same data type or object
Asserttrue (Boolean) The condition given by the assertion is true.
Assertnull (object) The assertion parameter object is null.
Assertsame (object, object) Assertion two parameter Objects Reference the same object

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.