JUnit Unit Test

Source: Internet
Author: User

JUnit Unit tests:

1. What is unit testing: After we have completed a project, we need to make a simple full-aspect test of its code logic, to see if the code logic is correct, for example, we wrote a log of the code, test, we will use the mobile phone number, account to log in to see if it can be boarded, if the wrong password or account to see if All these need to be tested, and the test will be handed in after the success. Testing is done in case the code goes wrong, and testing is an essential part of the project before it is handed in. The test code needs to "Learn Java, to the Kaige123.com Academy" we write ourselves, without junit we can also test (in the main method to invoke their own written test code can), but JUnit is a dedicated test plug-in, can help us to test more quickly and easily.

Installation configuration for 2.JUnit:

2.1Maven Engineering: Write a dependency directly in the POM. Such as:

2.2 General Engineering: with JUnit in Eclipse, general engineering can use the tool's own junit, or you can download the corresponding version of the jar package (two jar packages) on the Internet, copy it to the project, and then select the jar package that was copied in the project to add it to the build You can go to the path.

Versions of 3.JUNIT3 and 4: The two versions of JUNIT3 and JUNIT4 are quite different in use, and when tested with JUNIT3, the test class needs to inherit the TestCase class, and the method name of the test method must be preceded by test, otherwise this method will not be called. While JUNIT4 is annotated, we only need to write the annotations (@Test) before the method. Here we take the test of personal income tax as an example, the source code is as follows:

Test code for 3.1JUNIT3:

Analysis: (1) JUnit test does not need to write the main method;

(2) Method2 () did not write test before, so only TestMethod1 () was run.

Test code for 3.2JUNIT4:

Analysis: Just write the annotations will run, note that the method can not be static, otherwise it will error.

JUnit Unit Test

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.