JUnit4註解基本介紹

@After

If you allocate external resources in a Before method you need to release them after the test runs.Annotating a public void method with @After causes that method to be run after the Test method. All @After methods are guaranteed to run even if a Before or Test method throws an exception. The @After methods declared in superclasses will be run after those of the current class.

如果在@Before註解方法中分配了額外的資源,那麼在測試執行完後,需要釋放分配的資源。

使用@After註解一個public void方法會使該方法在@Test註解方法執行後被執行

即使在@Before註解方法、@Test註解方法中拋出了異常,所有的@After註解方法依然會被執行,見示例

父類中的@After註解方法會在子類@After註解方法執行後被執行`public class MathTest {
@Before
public void setUp() throws Exception {
throw new Exception();
}

@Test
public void testAdd() {
    Math m = new Math();
    assertTrue(m.add(1, 1) == 2);
}

@After
public void tearDown() throws Exception {
    System.out.println("after");
}

}
@AfterClass

If you allocate expensive external resources in a Before Class method you need to release them after all the tests in the class have run. Annotating a public static void method with @AfterClass causes that method to be run after all the tests in the class have been run. All @AfterClass methods are guaranteed to run even if a Before Class method throws an exception.The @AfterClass methods declared in superclasses will be run after those of thecurrent class.

如果在@BeforeClass註解方法中分配了代價高昂的額外的資源,那麼在測試類中的所有測試方法執行完後,需要釋放分配的資源。

使用@AfterClass註解一個public static void方法會使該方法在測試類中的所有測試方法執行完後被執行

即使在@BeforeClass註解方法中拋出了異常,所有的@AfterClass註解方法依然會被執行

父類中的@AfterClass註解方法會在子類@AfterClass註解方法執行後被執行

@Before

When writing tests, it is common to find that several tests need similar objects created before they can run. Annotating a public void method with @Before causes that method to be run before the Test method. The @Before methods of superclasses will be run before those of the current class. No other ordering is defined.

當編寫測試方法時,經常會發現一些方法在執行前需要創建相同的對象

使用@Before註解一個public void 方法會使該方法在@Test註解方法被執行前執行(那麼就可以在該方法中創建相同的對象)

父類的@Before註解方法會在子類的@Before註解方法執行前執行

@BeforeClass

Sometimes several tests need to share computationally expensive setup (like logging into a database). While this can compromise the independence of tests, sometimes it is a necessary optimization.Annotating a public static void no-arg method with @BeforeClass causes it to be run once before any of the test methods in the class. The @BeforeClass methods of superclasses will be run before those the current class.

有些時候,一些測試需要共享代價高昂的步驟(如數據庫登錄),這會破壞測試獨立性,通常是需要優化的

使用@BeforeClass註解一個public static void 方法,並且該方法不帶任何參數,會使該方法在所有測試方法被執行前執行一次,並且只執行一次

父類的@BeforeClass註解方法會在子類的@BeforeClass註解方法執行前執行

@Ignore

Sometimes you want to temporarily disable a test or a group of tests. Methods annotated with Test that are also annotated with @Ignore will not be executed as tests. Also, you can annotate a class containing test methods with @Ignore and none of the containing tests will be executed. Native JUnit 4 test runners should report the number of ignored tests along with the number of tests that ran and the number of tests that failed.

對包含測試類的類或@Test註解方法使用@Ignore註解將使被註解的類或方法不會被當做測試執行

JUnit執行結果中會報告被忽略的測試數`

public class MathTest {
    @Ignore("do not test")
    @Test
    public void testAdd() {
        Math m = new Math();
        assertTrue(m.add(1, 1) == 2);
    }
}

@Ignore
public class MathTest {
    @Test
    public void testAdd() {
        Math m = new Math();
        assertTrue(m.add(1, 1) == 2);
    }
}

執行結果相同:
@Test

The Test annotation tells JUnit that the public void method to which it is attached can be run as a test case. To run the method, JUnit first constructs a fresh instance of the class then invokes the annotated method. Any exceptions thrown by the test will be reported by JUnit as a failure. If no exceptions are thrown, the test is assumed to have succeeded.

The Test annotation supports two optional parameters.

The first, expected,declares that a test method should throw an exception. If it doesn’t throw an exception or if it throws a different exception than the one declared, the test fails.

The second optional parameter, timeout, causes a test to fail if it takes longer than a specified amount of clock time (measured in milliseconds).

@Test註解的public void方法將會被當做測試用例

JUnit每次都會創建一個新的測試實例,然後調用@Test註解方法

任何異常的拋出都會認爲測試失敗

@Test註解提供2個參數:

1,“expected”,定義測試方法應該拋出的異常,如果測試方法沒有拋出異常或者拋出了一個不同的異常,測試失敗

2,“timeout”,如果測試運行時間長於該定義時間,測試失敗(單位爲毫秒)
public class MathTest {
@Test(expected=Exception.class)
public void testAdd() throws Exception{
throw new Exception();
}
}

public class MathTest {
    @Test(timeout=5000)
    public void testAdd() {
        for(;;){

        }
    }
}

總結:
@After:釋放資源
@Test:測試方法,在這裏可以測試期望異常和超時時間
@Ignore:忽略的測試方法
@BeforeClass:針對所有測試,只執行一次,且必須爲static void
@AfterClass:針對所有測試,只執行一次,且必須爲static void
一個JUnit 4 的單元測試用例執行順序爲:
@BeforeClass –> @Before –> @Test –> @After –> @AfterClass
每一個測試方法的調用順序爲:
@Before –> @Test –> @After

本文參考:http://blog.csdn.net/a19881029/article/details/9668969
http://www.cnblogs.com/maxupeng/archive/2010/12/03/1895698.html

發佈了30 篇原創文章 · 獲贊 18 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章