Some JUnit Notes:
- Test methods must be modified to use @Test
- Public void test method must be modified, not arguments
- Unit tests are generally used to create a new test directory for testing code, just to test the code directory can be deleted at the time of production deployment
- Test code and the package should be consistent test code packet structure
- Each test unit independent method must be tested, there can be no dependencies between methods
- Test test class is generally used as the class name suffix
- The test method is generally used as a test method name prefix
Test failed Explanation:
- Failure: generally due to inconsistent test results and expected results triggered, indicating that the point of the test found a problem
- error: abnormal is caused by the code, it can generate an error in the test code itself, may be hidden in the test code bug
Some common notes:
- @Test: One common method is modified to a testing method
- @Test (excepted = xx.class): indicates when xx.class exception classes, representation of the test This exception is thrown, the test is considered normal by
- @Test (timeout = milliseconds): test method execution time is in line with expectations
- @BeforeClass: will be executed before the execution of all methods, static methods (globally only once, and was the first run)
- @AfterClass: will be executed, static method (globally only once, and the last run) after all of the execution method
- @Before: will be executed once before each test method is run
- @After: will be performed once after each test method run
- @Ignore: The modified test method will be ignored test runner
- @RunWith: You can change the test runner org.junit.runner.Runner
- Parameters: Parameter annotation
Parametric tests:
For a method requires numerous test scenarios, the workload may be reduced by testing parameterized test. Usage is as follows:
Package junit.util. 1; 2 . 3 Import static org.junit.Assert.assertEquals; . 4 . 5 Import java.util.Arrays; . 6 Import java.util.Collection; . 7 . 8 Import org.junit.Test; . 9 Import the org.junit. runner.RunWith; 10 Import org.junit.runners.Parameterized; . 11 Import org.junit.runners.Parameterized.Parameters; 12 is 13 is @RunWith (Parameterized.class) 14 {public class ParameterTest 15 16 / ** . 17 *. 1, change test runner is RunWith (Parameterized.class) 18 is 2 *, declare a variable used to store the expected value and the result value 19 * 3, a declared value Collection of public static method returns, and using modified @Parameters 20 * 4 bit public constructor with a class declaration test parameter, and in which a variable assignment statement is 21 * / 22 is 23 is the except int; // to store the expected result 24 int input1; // for storing a first input parameter 25 int input2; // for storing the second input parameter 26 is 27 @Parameters 28 public static Collection <Object []> initTestData () { 29 return Arrays.asList ( 30 new new Object [] [] { 31 is {3,1,2}, 32 {10,5,5}, 33 is} {6, 4, 34 is { }} 7,3,4 35); 36} 37 [ 38 is public ParameterTest (the except int, int INPUT1, INPUT2 int) { 39 = the except this.except; 40 this.input1 = INPUT1; 41 is this.input2 = INPUT2; 42 is} 43 44 45 46 is 47 48 @Test 49 public void testAdd() { 50 assertEquals(except, new Claculate().add(input1, input2)); 51 } 52 53 }