JUnit 4 uses the annotation (annotation) in Java 5. The following describes several annotation commonly used in JUnit 4.
@ Before: Initialization Method
@ After: release resources
@ Test: Test method. The expected exception and timeout can be tested here.
@ Ignore: Test Method ignored
@ Beforeclass: All tests are executed only once and must be static void
@ Afterclass: only one test is executed and must be static void.
The execution sequence of a unit test case of JUnit 4 is:
@ Beforeclass-> @ before-> @ test-> @ After-> @ afterclass
The call sequence of each test method is as follows:
@ Before-> @ test-> @ after
Write an example and test it.
Import static Org. JUnit. assert. *; import Org. JUnit. after; import Org. JUnit. afterclass; import Org. JUnit. before; import Org. JUnit. beforeclass; import Org. JUnit. ignore; import Org. JUnit. test; public class junit4test {
@ Before
Public void before (){
System. Out. println ("@ before ");
}
@ Test
Public void test (){
System. Out. println ("@ test ");
Assertequals (5 + 5, 10 );
}
@ Ignore
@ Test
Public void testignore (){
System. Out. println ("@ ignore ");
}
@ Test (timeout = 50)
Public void testtimeout (){
System. Out. println ("@ test (timeout = 50 )");
Assertequals (5 + 5, 10 );
}
@ Test (expected = arithmeticexception. Class)
Public void testexpected (){
System. Out. println ("@ test (expected = Exception. Class )");
Throw new arithmeticexception ();
}
@ After
Public void after (){
System. Out. println ("@ after ");
}
@ Beforeclass
Public static void beforeclass (){
System. Out. println ("@ beforeclass ");
};
@ Afterclass
Public static void afterclass (){
System. Out. println ("@ afterclass ");
};
};
Output result
@ Beforeclass
@ Before
@ Test (timeout = 50)
@ After
@ Before
@ Test (expected = Exception. Class)
@ After
@ Before
@ Test
@ After
@ Afterclass
@ Beforeclass and @ afterclass |
@ Before and @ after |
It can only appear once in a class |
A class can appear multiple times, that is, the two annotaion labels can be added before the declaration of multiple methods, and the execution sequence is uncertain. |
No restrictions on method names |
No restrictions on method names |
Run only once in the class |
Run Once before or after each test method. |
@ Beforeclass the method that identifies the annotation in the parent class will be executed before the method that identifies the annotation in the current class. @ Afterclass the method that identifies the annotation in the parent class will be executed after the method that identifies the annotation in the current class |
@ Before the method that identifies the annotation in the parent class will be executed before the method that identifies the annotation in the current class. @ After the method that identifies the annotation in the parent class will be executed after the method that identifies the annotation in the current class |
Must be declared as public static |
Must be declared public and non-static |
All methods marked as @ afterclass will be executed, even if an exception is thrown by the @ beforeclass method. |
All methods marked as @ after will be executed, even if an exception is thrown by the @ before or @ test method. |
@ Beforeclass and @ afterclass are effective for the allocation or release of expensive resources because they are only executed once in the class. In contrast, using @ before and @ After is also a wise choice for resources that need to be initialized or cleared before each run.
From: http://wwwz.iteye.com/blog/760028