In the unit Test in Java: @ BeforeClass, @ Before, @ Test, @ After, @ AfterClass

Source: Internet
Author: User

In Junit4, there are also Test Annotations: @ BeforeClass, @ Before, @ Test, @ After, @ AfterClass1, where: @ BeforeClass, @ AfterClass is newly added to Junit4. 2. If Run as ---> Junit Test runs the method containing @ Test annotations, all annotation methods will be executed, the execution sequence is: @ BeforeClass, @ Before, @ Test, @ After, @ AfterClass3. In JUnit4, if the Test class inherits the TestCase class, all Annotation functions. 4. Case: package cn.edu. hactcm. testjunit; import java. util. UUID; import org. junit. after; import org. junit. afterClass; import org. junit. before; import org. junit. beforeClass; import org. junit. test;/*** author: toto 11111111@qq.com * CFMS: Computer files management system * version: 1.0 01:26:04 ** methods used in unit test */public class TestJunit {private String flag;/*** run the command before loading the class, note that the Keyword: static */@ BeforeClass public static void testBeforeClass () {System. out. println ("public static void testBeforeClass ()");}/*** run the */@ Before public void testBefore () {System Before the target test method testTest. out. println ("-----------------------------------"); System. out. println ("public void testBefore ()"); flag = UUID. randomUUID (). toString (); System. out. println ("-----------------------------------");}/*** target Test method testTest () */@ Test public void testTest () {System. out. println ("public void testTest ()"); System. out. println (flag); flag = null;}/*** target Test method testTest2 () */@ Test public void testTest2 () {System. out. println ("public void testTest2 ()"); System. out. println (flag); flag = null;}/*** run the test method testTest () */@ After public void testAfter () {System. out. println ("-----------------------------------"); System. out. println ("public void testAfter ()"); System. out. println (flag); System. out. println ("---------------------------------");}/*** after loading this class, note that the Keyword: static */@ AfterClass public static void testAfterClass () {System. out. println ("public static void testAfterClass ()");} www.2cto.com} Only executes the testTest () method and the result is: public static void testBeforeClass () implements public void testBefore () export public void testTest () using ------------------------------------- public void testAfter () null ------------------------------------- public static void testAfterClass ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.