標籤:des style blog http io os 使用 java ar
1 junit測試案例代碼
package junitTest;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;/** * @ClassName TestCase * @Description junit測試案例示範 * @date 2014年9月11日 上午9:57:45 * */public class TestCase { // @Test // public void test() { // fail("Not yet implemented"); // } /** * * @Title setUpBeforeClass * @Description 針對所有測試,只執行一次,在該class執行前執行,且必須為static void * @param * @return * @throws */ @BeforeClass public static void setUpBeforeClass() throws Exception { System.out.println("in BeforeClass================"); } /** * * @Title tearDownAfterClass * @Description 針對所有測試,只執行一次,在該class執行完後執行,且必須為static void * @param * @return * @throws */ @AfterClass public static void tearDownAfterClass() throws Exception { System.out.println("in AfterClass================="); } /** * * @Title before * @Description 初始化測試方法,每個測試方法都需要調用一次 * @param * @return * @throws */ @Before public void before() { System.out.println("in Before"); } /** * * @Title after * @Description 測試方法執行完後釋放資源,每個測試方法都需要調用一次 * @param * @return * @throws */ @After public void after() { System.out.println("in After"); } @Test(timeout = 10000) public void testadd() { JDemo a = new JDemo(); assertEquals(6, a.add(3, 3)); System.out.println("in Test ----Add"); } @Test public void testdivision() { JDemo a = new JDemo(); assertEquals(3, a.division(6, 2)); System.out.println("in Test ----Division"); } @Ignore @Test public void test_ignore() { JDemo a = new JDemo(); assertEquals(6, a.add(1, 5)); System.out.println("in test_ignore"); } @Test public void test_fail() { fail(); }}/** * * @ClassName JDemo * @Description 測試類別 * @date 2014年9月11日 上午10:15:15 * */class JDemo extends Thread { int result; public int add(int a, int b) { try { sleep(1000); result = a + b; } catch (InterruptedException e) { System.out.println("add error"); } return result; } public int division(int a, int b) { return result = a / b; }}
2 java執行結果
in BeforeClass================
in Before
in Test ----Division
in After
in Before
in After
in Before
in Test ----Add
in After
in AfterClass=================
3 junit執行結果
4 執行順序
一個測試類別單元測試的執行順序為:
@BeforeClass –> @Before –> @Test –> @After –> @AfterClass
每一個測試方法的調用順序為:
@Before –> @Test –> @After
5 斷言核心方法
| assertArrayEquals(expecteds, actuals) |
查看兩個數組是否相等。 |
| assertEquals(expected, actual) |
查看兩個對象是否相等。類似於字串比較使用的equals()方法 |
| assertNotEquals(first, second) |
查看兩個對象是否不相等。 |
| assertNull(object) |
查看對象是否為空白。 |
| assertNotNull(object) |
查看對象是否不為空白。 |
| assertSame(expected, actual) |
查看兩個對象的引用是否相等。類似於使用“==”比較兩個對象 |
| assertNotSame(unexpected, actual) |
查看兩個對象的引用是否不相等。類似於使用“!=”比較兩個對象 |
| assertTrue(condition) |
查看運行結果是否為true。 |
| assertFalse(condition) |
查看運行結果是否為false。 |
| assertThat(actual, matcher) |
查看實際值是否滿足指定的條件 |
| fail() |
讓測試失敗 |
6 junit方法聲明
| @Before |
初始化方法 |
| @After |
釋放資源 |
| @Test |
測試方法,在這裡可以測試期望異常和逾時時間 |
| @Ignore |
忽略的測試方法 |
| @BeforeClass |
針對所有測試,只執行一次,且必須為static void |
| @AfterClass |
針對所有測試,只執行一次,且必須為static void |
| @RunWith |
指定測試類別使用某個運行器 |
| @Parameters |
指定測試類別的測試資料集合 |
| @Rule |
允許靈活添加或重新定義測試類別中的每個測試方法的行為 |
| @FixMethodOrder |
指定測試方法的執行順序 |
參考檔案:http://blog.csdn.net/wangpeng047/article/details/9628449
junit入門執行個體