when we write large programs, we need to write thousands of methods or functions that may be powerful, but we only use a small subset of the functions of the function in our program, and by debugging we can determine that a small subset of the functionality is correct. However, we should also make sure that each function is completely correct, because if we extend the program in the future and use the other functions of a function, which is a bug , it is definitely a very depressing thing. So, after each function is written, all aspects of the function should be tested, which we call unit tests. (Simple to understand is to test each method)
First, we build a test engineering jutiltes, and then create a method that needs to be tested, the code is as follows.
Package Util; Public class MyMath { publicstaticint sum (int A,int b) { return A +b ; } Public Static int divde (int A,int b) { return A/b; }}
The second step is toThe JUnit4 Unit Test package introduces this project: Right-click on the item, point "Properties",
In the pop-up Properties window, first select " Java Build Path, then to the right, select the "Libraries" tab, then on the far right, click " Add Library ... "button, as shown in:
then, in the New Popup dialog box, select JUnit4 and click OK, as shownin theJUnit4 software package is included in our project.
The third step is to generatejunit Test framework: In Eclipse's package Explorer, right-click on the pop-up menu and select "Newà JUnit test case". As shown in the following:
In the popup dialog box, make the appropriate selections as shown in:
In the popup dialog box, click the bottom Next button to jump, as shown in:
After four, the system will automatically generate a new class Mymathtest, which contains some empty test cases. You only need to make these test cases slightly modified to use. The complete mymathtest code is as follows:
PackageUtil;Import Staticorg.junit.assert.*;Importorg.junit.Test;/** Check the method and click Runtest otherwise the whole method will be run*/ Public classmymathtest {@Test Public voidtestsum () {intActual=mymath.sum (10, 10);//Actual value intexpected=20;//expectationAssertequals (actual,expected);//Assertion} @Test Public voidTestdivde () {intActual=mymath.divde (10, 10);//Actual value intexpected=20;//expectationAssertequals (actual,expected);//Assertion } }
V. Operation and Knot
Unit testing with JUNIT4 in Eclipse (beginner)