Junit4學習(三)Junit運行流程

一,驗證Junit測試方法的流程數據庫

1,在test/com.duo.util右鍵,新建測試類測試

2,生成後的代碼:spa

 1 package com.duo.util;
 2 
 3 import static org.junit.Assert.*;
 4 
 5 import org.junit.After;
 6 import org.junit.AfterClass;
 7 import org.junit.Before;
 8 import org.junit.BeforeClass;
 9 import org.junit.Test;
10 
11 public class JunitFlowTest {
12 
13     @BeforeClass
14     public static void setUpBeforeClass() throws Exception {
15         System.out.println("This is @BeforeClass...");
16     }
17 
18     @AfterClass
19     public static void tearDownAfterClass() throws Exception {
20         System.out.println("This is AfterClass...");
21     }
22 
23     @Before
24     public void setUp() throws Exception {
25         System.out.println("This is Before...");
26     }
27 
28     @After
29     public void tearDown() throws Exception {
30         System.out.println("This is After...");
31     }
32     
33     @Test
34     public void test1(){
35         System.out.println("This is test1...");
36     }
37     
38     @Test
39     public void test2(){
40         System.out.println("This is test2...");
41     }
42 
43 }

運行結果:code

This is @BeforeClass...
This is Before...
This is test1...
This is After...
This is Before...
This is test2...
This is After...
This is AfterClass...

二,總結:blog

1,@BeforeClass修飾的方法會在全部方法被調用前被執行;並且該方法是靜態的,因此當測試類被加載後接着就會運行它,並且在內存中它只會存在一份實例,它比較適合加載配置文件;好比數據的鏈接文件等;內存

2,@AfterClass所修飾的方法一般用來對資源的清理,如數據庫的關閉;資源

3,@Before和@After會在每一個測試方法先後執行;一般被稱爲固定代碼(fixure),就是必定會執行的代碼.it

相關文章
相關標籤/搜索