A very convenient testing method-JUnit unit testing (IDEA)

One, what is JUnit

JUnit is a unit testing framework for the Java programming language. JUnit has a very important development in test-driven development. It is one of the unit testing frameworks collectively called xUnit that originated from JUnit.

2. Why use it

  1. Can write a series of test methods, unit test all interfaces or methods of the project
  2. There is no need to create multiple main methods in the src file for testing.
  3. After starting, automate the test and judge the execution result without human intervention.
  4. Add, delete, and shield test methods without affecting other test methods. Open source frameworks all have corresponding support for JUnit.
  5. You can view the running time and tune it according to the corresponding operation.

Three, how to implement JUnit unit testing

The first step is to create a lib file and import the junit-4.8.jar file into it.

Click file and select Project Structure, click junit-4.8.jar for free download .
Insert picture description here
After opening the Project Structure, then open Libraries, select + sign -> Java -> name to add project -> + sign -> import the junit-4.8.jar file.

Insert picture description here
Next, select the Modeules model, add the project file to it according to the process, and select Test.
Insert picture description here
Insert picture description here
Finally, create the file test. At this time, the file is gray. Right-click the file and select Mark Directory as-> Test resource RootChange to Test File. The file will turn green.
Insert picture description here
Then you can press the shortcut key to shift +ctrl + tselect the method to be tested in the Class class in the project file, and the test class will be automatically generated in the green test file.
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/mjh1667002013/article/details/114755353