Junit 4 Core Concept [1] Annotations

来源:互联网 发布:php timestamp 格式化 编辑:程序博客网 时间:2024/06/06 02:26

All contents of the article are extracted from the book JUnit In Action, 2nd edition by Petar Tahchiev etc.


The requirements to define a test class 

are that the class must be public and contain a zero-argument constructor.


The requirements to create a test method 

are that it must be annotated with@Test, be public, take no arguments, and return void.


Independecy of Each Test Method

JUnit creates a new instance of the test class before invoking each @Test method.This helps provide independence between test methods and avoids unintentional side effects in the test code. Because each test method runs on a new test class instance,we can’t reuse instance variable values across test methods

@Before and @After Annotation

The @Before and @After annotated methods are executed right before/after the execution of each oneof your @Test methods and regardless of whether the test
failed or not.

 You can have as many of these methods as you want, but beware thatif you have more than one of the @Before/@After methods,the order of their execution is not defined.

@BeforeClass and @AfterClass annotations

the @BeforeClass and @AfterClass annotations to annotate your methods in that class. The methods that you annotate willget executed, only once, before/after all of your @Test methods. you can have as many of these methods as you want, and againthe order of the execution is unspecified. 

 the @Before/@After annotated methods must be public. The @BeforeClass/@AfterClass annotated methods must be public and static.

0 0