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