Use JUnit in IDEA 2017.2.2 environment, 2017.2.2junit
JUnit: unit test framework. The test object is a method in a class.
JUnit is not part of Javase. To use the jar package that needs to be imported, you must use the JUnit plug-in IntelliJ IDEA.
JUnit version 3.X 4.X
I. JUnit environment Configuration:
1. Plug-in loading Selection
2. Search for the plug-in JUnit and download Grails, JUnit, and JUnitGenrator V2.0. skip this step if you have downloaded the plug-in.
3. Select the loading plug-ins Grails, JUnit, and JUnitGenrator V2.0.
4. Click OK in the lower-right corner of the screen and restart IDEA to load the plug-in.
Ii. Simple unit test example
1. Create a package named testjunit under src and create a class in the package.
2. Create a folder named test in the src directory at the same level, right-click and select Test Sources Root,
Create a package with the same name as the testjunit package in the folder and create a test class.
3. Create a test method in TestDemo. class:
① Method naming rules for unit test methods: public void method name (){}
Note that the method name cannot have parameters.
② Run the Test method in Annotation mode. On the Method: @ Test; if there is no annotation, a running exception occurs.
③ Select a method name, right-click debugging, and press ctrl + shift + D.
④ If a green bar appears, the method test is successful.
If a red bar appears, the test method fails.
Iii. Automatic Unit Testing
1. automatically generate a unit test. The shortcut key is Command + Shift + T.
2. Select related configurations
3. automatically generate the following template
4. Run the test
Iv. Annotations
@ Test: indicates the unit Test of the method.
@ Ignore: indicates that no unit test is performed for the method.
@ Before: run the command Before each method.
@ After: Run After each method