JUnit 4 usage

Source: Internet
Author: User

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

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.