import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author redcrow
*/
public class UnitTestLifeCycle {
private static final Logger LOG = LoggerFactory.getLogger(UnitTestLifeCycle.class);
public UnitTestLifeCycle() {
LOG.debug("Contructor : UnitTestLifeCycle()");
}
@BeforeClass
public static void setUpClass() {
LOG.debug("@BeforeClass : setUpClass()");
}
@AfterClass
public static void tearDownClass() {
LOG.debug("@AfterClass : tearDownClass()");
}
@Before
public void setUp() {
LOG.debug("@Before : setUp()");
}
@After
public void tearDown() {
LOG.debug("@After : tearDown()");
}
@Test
public void testMethod1() {
LOG.debug("@Test : testMethod1()");
}
@Test
public void testMethod2() {
LOG.debug("@Test : testMethod2()");
}
@Test
public void testMethod3() {
LOG.debug("@Test : testMethod3()");
}
@Test
public void testMethod4() {
LOG.debug("@Test : testMethod4()");
}
@Test
public void testMethod5() {
LOG.debug("@Test : testMethod5()");
}
}
@BeforeClass : setUpClass() จะทำงานครั้งเดียวเท่านั้น และทำเป็นอันแรกสุดเสมอ (ก่อน test method(@Test) ทั้งหมด) หลังจากที่เรา Test file นั้นๆ ซึ่ง method นี้เราจะเอาไว้ set up ค่าต่างๆ ค่าที่ต้องการให้มันเริ่มต้นตอน load class แค่ครั้งเดียวเท่านั้น
@AfterClass : tearDownClass() คล้ายๆ @BeforeClass คือทำงานครั้งเดียว แต่จะทำหลังสุด หลังจากที่ test method(@Test) ทั้งหมด ทำงานเสร็จสิ้นแล้ว เอาไว้เคลียร์ค่า หรือคืนค่าบางอย่างกลับคืนสู่ระบบ
@Before : setUp() จะทำงานก่อนทุกๆ test method(@Test) เสมอ ทำงานใหม่ในทุกๆ test method เอาไว้กำหนดค่าเริ่มต้น ก่อนที่ test method ถัดไปจะเริ่มต้นทำงาน
@After : tearDown() คล้ายๆ @Before แต่จะทำงานใหม่หลังทุกๆ test method(@Test) เสมอ ไว้เคลียร์ค่า หรือคืนค่าบางอย่าง ก่อนที่ test method ถัดไปจะเริ่มต้นทำงาน
Constructor : UnitTestLifeCycle() จะทำงานก่อน @Before ของทุกๆ test method
ใน 1 Unit Test เราสามารถมี test method(@Test) กี่ method ก็ได้ และจะไม่ขึ้นตรงต่อกัน (ไม่เรียงลำดับ)

ไม่มีความคิดเห็น:
แสดงความคิดเห็น