JUNIT4 using annotations in Java5 (annotation), here are a few annotation commonly used by JUNIT4:
@Before: Initialization method is performed once for each test method (note the difference from beforeclass , which is performed once for all methods )
@After: Releasing resources is performed once for each test method (note the difference from Afterclass, which is performed once for all methods )
@Test: Test method, where you can test for expected exceptions and time-outs
@Test (Expected=arithmeticexception.class) checks if the method being tested throws an ArithmeticException exception
@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 JUNIT4 unit test case Execution sequence is:
@AfterClass @After, @Test, @Before, @BeforeClass
the order in which each test method is called is:
@After, @Test, @Before
public class junit4test { @Before public void before () { System.out.println ("@Before"); } @Test /** *mark your test cases with @Test annotations. *you don ' t need to prefix your test cases with "Test". *tested class does not need to extend from "TestCase" class. */ 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, ; } @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 =)
@After
@Before
@Test (expected = 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 for that The allocation of more "expensive" resources or release is very effective, because they will only be 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.
The difference between @after @before of JUNIT4 in @afterclass @BeforeClass