Unit 4 uses annotations in Java 5 (annotation), the following is a few annotation introduced by JUnit 4
@Before: Initialization method
@After: Freeing Resources
@Test: Test method, where you can test for expected exceptions and time-outs
@Ignore: Ignored test methods
@BeforeClass: For all tests, execute only once and must be static void
@AfterClass: For all tests, execute only once and must be static void
A JUnit 4 unit test Case execution sequence is:
@BeforeClass –> @Before –> @Test –> @After –> @AfterClass
The order in which each test method is called is:
@Before –> @Test –> @After
Write an example, test it, test it.
Public classjunit4test {@Before Public voidbefore () {System.out.println ("@Before");} @Test Public voidTest () {System.out.println ("@Test"); Assertequals (5 + 5, 10);} @Ignore @test Public voidTestignore () {System.out.println ("@Ignore");} @Test (Timeout= 50) Public voidtesttimeout () {System.out.println ("@Test (timeout = 50)"); Assertequals (5 + 5, 10);} @Test (Expected= ArithmeticException.class) Public voidtestexpected () {System.out.println ("@Test (expected = exception.class)"); Throw Newarithmeticexception ();} @After Public voidAfter () {System.out.println ("@After"); } @BeforeClass Public Static voidBeforeclass () {System.out.println ("@BeforeClass"); }; @AfterClass Public Static voidAfterclass () {System.out.println ("@AfterClass"); };};
Output results
= = Exception. class ) @After @before@test@after@afterclass
@BeforeClass and @AfterClass |
@Before and @After |
Only one time can appear in a class |
can occur multiple times in a class, that is, you can add these two annotaion tags before the declaration of multiple methods, and the order of execution is indeterminate |
Method name does not limit |
Method name does not limit |
Run only once in a class |
Run once before or after each test method |
@BeforeClass method that identifies the annotation in the parent class will be executed before the method that identifies the annotation in the current class. @AfterClass method that identifies the annotation in the parent class will execute after the method that identifies the annotation in the current class |
@Before method that identifies the annotation in the parent class will be executed before the method that identifies the annotation in the current class. @After method that identifies the annotation in the parent class will execute after the method that identifies the annotation in the current class |
Must be declared as public static |
Must be declared as public and not static |
All methods that are identified as @afterclass are bound to be executed, even if the method that is identified as @beforeclass throws an exception. |
All methods identified as @after are bound to be executed, even if the method that identifies the @Before or @Test throws an exception. |
@BeforeClass and @AfterClass are very effective for allocating or releasing resources that are "expensive", 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 before each run, or need to be cleaned after running.
Before after Ignore Test Beforeclass Afterclass in JUnit 4