1. The object being tested is a method in a class.
2. Import the jar package.
3, Unit testing method, the test method naming rules for the public void Method name () {}, Note: The test class cannot be named public class Test{},test and @Test names will be reported Type Mismatch:cannot Co Nvert from Test to Annotation
Create the source folder folder, test the package name of the class and the package name of the method being tested
4, the use of annotations to run the test method, the test method is added above @Test
5. Choose the test method name right-click Run As→junit test.
The code is as follows:
method to be tested:
1 Package Com.junit; 2 3 Public class Junit {4 Public void sum (int A,int b) {5 System. out. println (A +b); 6 }7 }
Test method:
1 PackageCom.junit;2 3 Importorg.junit.Test;4 5 Public classTest1 {6 @Test7 Public voidtest1 () {8JUnit JUnit =NewJunit ();9Junit.sum ();Ten } One}
For example, a green bar indicates a successful unit test, and a red bar indicates a failure
@Ignore: Indicates that unit tests are not run
@Before: Run Before method
@After: Run After method
Assertion:
Assert Assertequals (expected, actual value).
Unit tests (JUnit use)