Simple configuration and experiences of JUnit

Source: Internet
Author: User

1. Configuration and use

On the project name for JUnit, click Properties -- Java build path-libraries, click Add external jars, and click the JUnit package.

Create a new JUnit test case on the project to be tested.

The test method is as follows:
Assertequals (World. sayhello (), "Hello World ");
Assert. assertequals (World. Add (1, 2), 4 );

 

2. Experiences:

The use of JUnit is not very difficult, but it is not easy to write a good testcase. A bad testcase often wastes time and does not play a practical role. On the contrary, a good testcase can not only point out the problems in the code, but also serve as a more accurate document of the code. It also plays a very important role in the process of continuous integration. Note the following points when writing testcase:

-Test independence: Only one object is tested at a time to locate the error location. This has two meanings: A testcase, only tests one object; A testmethod, only tests one method in this object.

-Give the test method a proper name.

-The reason for the failure is given in the assert function, for example, asserttrue ("… Shocould be true ",......), To facilitate troubleshooting. In this example, if asserttrue cannot be used, the message is displayed. In JUnit, each assert function has the first parameter, which is the prototype of the function that displays the message when an error occurs.

-Test all possible failures, for example, functions frequently modified in a class. For classes that only contain getter/setter, if they are generated by IDE (such as Eclipse), it is not expected. If it is manually written, it is best to test it.

-The code in setup and teardown should not be related to the test method, but should be globally related. For example, for Methods A and B, the code in setup and teardown should be the code required by both A and B.

-Test code organization: the same package and different directories. In this way, the test code can access the protected variable/method of the tested class to facilitate the compilation of the test code. It facilitates the management of test code and the packaging and release of codes in different directories. An example is as follows:

SRC <= source code root directory

-Com

-Mod1

-Class1

JUnit <= test code root directory

-Com

-Mod1

-Class1

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.