Basic introduction
JUnit is a unit testing framework for the Java language. It was founded by Kent Beck and Erich Gamma and gradually became the most successful of the xUnit family derived from Kent Beck's sUnit. JUnit has its own JUnit extension ecosystem. Most Java development environments have integrated JUnit as a unit testing tool. JUnit is a regression testing framework written by Erich Gamma and Kent Beck. Junit testing is programmer testing, the so-called white-box testing, because programmers know how (How) the software being tested completes its functions and what (What) functions it completes. Junit is a set of frameworks. If you inherit the TestCase class, you can use Junit for automatic testing.
Content screenshot
Import method
Introduce the method of configuring practical JUnit4 in Eclipse (the same is true for MyEclipse).
1. Right-click on your project and select Properties at the bottom, select Java Build Path in the navigation bar on the left side of the pop-up window, and select Libraries on the upper tab;
2. Then click Add Library... on the right, select JUnit in the pop-up window and click Next>;
3. JUnit 4 is selected by default, and then Finish;
In this way, the JUnit 4 test toolkit is added.
Test method
First, right-click on the class file we want to test->New->Junit Test Case, and a dialog box for creating a new JUnit test case will pop up. You can select the JUnit version, path name and a series of information here, or you can use By default, we select the setUp() and tearDown() functions to perform initialization and garbage collection based on the default configuration;
Then click Next> to select the functions that need to be tested in the class. We select the four functions plus, minus, times, and divide that we wrote before;
Click Finish, and a test class named TestCalculate will automatically be created, which contains empty test cases for the corresponding functions. Now you only need to fill in the code of the test content you want to perform.
After that, we right-click Run As->JUnit Test on the test class to run the test and the results pop up on the left.
it works
it works
it works