JUnit unit testing

JUnit unit tests:

1. What is unit testing: After we complete a project, we need to conduct a simple and comprehensive test of its code logic to see if the code logic is correct. For example, we have written a piece of login code. When testing, we must separate Use the mobile phone number and account to log in to see if you can log in. If you enter the wrong password or account to see if an error will be reported, we need to test these. After the test is successful, you can submit it. Testing is just in case the code will go wrong, and testing is an essential part of the project before it is handed in. The test code needs to [Learn Java, go to Kaige School kaige123.com ] We write it ourselves, we can test it without JUnit (just call the test code written by ourselves in the main method), but JUnit is a plug-in specially used for testing , which can help us to test more quickly and conveniently.

2. JUnit installation configuration:

2.1 Maven project: Just write the dependencies directly in the pom. As shown below:

image

2.2 Ordinary project: JUnit comes with eclipse. Ordinary projects can use the JUnit that comes with the tool, or you can download the corresponding version of the jar package (two jar packages in the above picture) from the Internet, copy it to the project, and then select it The jar package copied in the project can be added to the Build Path.

3. The difference between the versions of JUnit3 and 4: The two versions of JUnit3 and JUnit4 are quite different in use. When using JUnit3 for testing, the test class needs to inherit the TestCase class, and the method name of the test method must be prefixed with test, Otherwise this method will not be called. And JUnit4 uses the annotation method, we only need to write the annotation (@Test) before the method. Let's take the test of personal income tax as an example. The source code is as follows:

image

3.1 JUnit3 test code:

image

image

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

(2) There is no test written before method2(), so only testmethod1() is run.

3.2 JUnit4 test code:

image

image

Analysis: As long as the annotation is written, it will run. Note that the method cannot be static, otherwise an error will be reported.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326974749&siteId=291194637
Recommended