<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 帶有示例的 JUnit 注釋教程 > 原文: [https://www.guru99.com/junit-annotations-api.html](https://www.guru99.com/junit-annotations-api.html) ## 什么是 JUnit 注釋? **JUNIT 注釋**是語法元數據的一種特殊形式,可以將其添加到 Java 源代碼中,以提高代碼的可讀性和結構性。 可以注釋變量,參數,包,方法和類。 注釋是在 Junit4 中引入的,它使 Java 代碼更具可讀性和簡單性。 這是 Junit3 和 Junit4 之間最大的區別,因為 Junit4 是基于注釋的。 借助 Junit5 中的注釋知識,您可以輕松學習和實現 JUnit 測試。 以下是重要和常用注釋的列表: | **編號** | **批注** | **說明** | | 1. | @測試 | 此注釋是 org.junit.TestCase 的替換,它指示它所附加的 public void 方法可以作為測試用例執行。 | | 2. | @之前 | 如果要在每個測試用例之前執行一些語句(例如前提條件),則使用此批注。 | | 3. | @課前 | 如果您想在所有測試用例之前執行某些語句,例如 測試連接必須在所有測試用例之前執行。 | | 4. | @后 | 如果您想在每個[測試用例](/test-case.html)之后執行一些語句,例如重置變量,刪除臨時文件,變量等,則可以使用此注釋。 | | 5. | @下課以后 | 如果您想在所有測試用例之后執行一些語句,例如,可以使用此注釋。 執行所有測試用例后釋放資源。 | | 6. | @忽略 | 如果您想在測試執行期間忽略某些語句,例如,可以使用此注釋。 在測試執行期間禁用一些測試用例。 | | 7. | @Test(超時= 500) | 如果您想在測試執行期間設置一些超時時間(例如, 如果您正在使用某些 SLA(服務水平協議),并且測試需要在指定的時間內完成。 | | 8. | @Test(expected = IllegalArgumentException.class) | 如果要在測試執行期間處理某些異常,可以使用此批注。 例如,如果要檢查特定方法是否拋出指定的異常。 | 在本教程中,您將學習- * [JUnit 注釋示例](#7) * [JUnit 聲明類](#2) * [JUnit 測試用例類](#3) * [JUnit TestResult 類](#4) * [JUnit 測試套件類](#5) ## JUnit 注釋示例 讓我們用簡單的打印語句創建一個覆蓋重要的 JUnit 批注的類,并使用測試運行器類執行它: **步驟 1)**考慮下面的 java 類,該類具有各種方法,這些方法附加到上面列出的注釋中: **JunitAnnotationsExample.java** ``` package guru99.junit; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import java.util.ArrayList; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; public class JunitAnnotationsExample { private ArrayList<String> list; @BeforeClass public static void m1() { System.out.println("Using @BeforeClass , executed before all test cases "); } @Before public void m2() { list = new ArrayList<String>(); System.out.println("Using @Before annotations ,executed before each test cases "); } @AfterClass public static void m3() { System.out.println("Using @AfterClass ,executed after all test cases"); } @After public void m4() { list.clear(); System.out.println("Using @After ,executed after each test cases"); } @Test public void m5() { list.add("test"); assertFalse(list.isEmpty()); assertEquals(1, list.size()); } @Ignore public void m6() { System.out.println("Using @Ignore , this execution is ignored"); } @Test(timeout = 10) public void m7() { System.out.println("Using @Test(timeout),it can be used to enforce timeout in JUnit4 test case"); } @Test(expected = NoSuchMethodException.class) public void m8() { System.out.println("Using @Test(expected) ,it will check for specified exception during its execution"); } } ``` **步驟 2)**讓我們創建一個測試運行器類以執行上述測試: **TestRunner.java** ``` package guru99.junit; import org.junit.runner.JUnitCore; import org.junit.runner.Result; import org.junit.runner.notification.Failure; public class TestRunner { public static void main(String[] args) { Result result = JUnitCore.runClasses(JunitAnnotationsExample.class); for (Failure failure : result.getFailures()) { System.out.println(failure.toString()); } System.out.println("Result=="+result.wasSuccessful()); } } ``` **預期結果** * 所有測試用例將一個接一個地執行,并且所有打印語句都可以在控制臺上看到。 * 如上表@Before 中所述,@BeforeClass [方法 m1()和 m2()]將分別在每個測試用例之前和所有測試用例之前執行。 * 以相同的方式,@ after,@ afterClass(方法 m3()和 m4())將分別在每個測試用例之后和所有測試用例之后執行。 @ignore(方法 m6())將被視為忽略測試。 讓我們詳細分析上述 java 類中使用的測試用例: 1. 考慮如下方法 m5(): ``` @Test public void m5() { list.add("test"); assertFalse(list.isEmpty()); assertEquals(1, list.size()); } ``` 在上面的方法中,因為您要在變量“列表”中添加一個字符串,所以 * **list.isEmpty()**將返回 false。 * **assertFalse(list.isEmpty())**必須返回 true。 * 結果,測試用例將通過**通過**。 由于您僅在列表中添加了一個字符串,因此大小為 1。 * **list.size()**必須將 int 值返回為“ 1”。 * 因此 **assertEquals(1,list.size())**必須返回 true。 * 結果,測試用例將通過**通過**。 2. 考慮如下所示的方法 m7(): ``` @Test(timeout = 10) public void m7() { System.out.println("Using @Test(timeout),it can be used to enforce timeout in JUnit4 test case"); } ``` 如上所述, **@Test(timeout = 10)**注釋用于在測試用例中強制超時。 3. 考慮如下所示的方法 m8(): ``` @Test(expected = NoSuchMethodException.class) public void m8() { System.out.println("Using @Test(expected) ,it will check for specified exception during its execution"); } ``` 如上所述, **@Test(expected)**將在執行期間檢查指定的異常,因此方法 m8()將拋出“無此類方法異常”。 結果,將異常執行測試。 通過所有測試用例后,這將導致成功執行測試。 **實際結果** 由于上面的示例中有三個測試用例,因此所有測試用例將一一執行。 參見下面的輸出**:** ![JUnit Annotations & API](https://img.kancloud.cn/d4/f9/d4f99d1fa80610ba899903ab52eaf203_430x188.png "JUnit Annotations & API") **請參閱下面的打印語句,可以在控制臺上看到它們:** 使用@BeforeClass,在所有測試用例之前執行 使用@Before 批注,在每個測試用例之前執行 使用@After,在每個測試用例之后執行 Using @Before annotations, executed before each test cases 使用@Test(timeout),它可以用于在 JUnit4 測試用例中強制執行超時 Using @After, executed after each test cases Using @Before annotations, executed before each test cases 使用@Test(expected),它將在執行過程中檢查指定的異常 Using @After, executed after each test cases 使用@AfterClass,在所有測試用例之后執行 ## JUnit 聲明類 此類提供了一些斷言方法,可用于編寫測試用例。 如果所有的 assert 語句都通過,則測試結果成功。 如果任何 assert 語句失敗,則測試結果將失敗。 如前所述,下表描述了重要的 Assert 方法和說明: | **編號** | **方法** | **說明** | | 1\. | void assertEquals(期望的布爾值,實際的布爾值) | 它檢查兩個值是否等于 Object 類的 equals 方法 | | 2\. | void assertFalse(布爾條件) | 功能是檢查條件是否為假。 | | 3\. | void assertNotNull(Object object) | “ assertNotNull”功能是檢查對象是否不為空。 | | 4\. | void assertNull(Object 對象) | “ assertNull”功能是檢查對象是否為空。 | | 5\. | void assertTrue(布爾條件) | “ assertTrue”功能是檢查條件是否為真。 | | 6\. | 無效 fail() | 如果要拋出任何斷言錯誤,則具有 fail()總是會導致失敗判定。 | | 7\. | void assertSame([字符串消息] | “ assertSame”功能是檢查兩個對象是否引用相同的對象。 | | 8\. | void assertNotSame([字符串消息] | “ assertNotSame”功能是檢查兩個對象沒有引用相同的對象。 | ## JUnit 測試用例類 要運行多個測試,可以在 **org.junit.TestCase** 程序包中使用 TestCase 類。 注釋@Test 告訴 JUnit,可以將它附加到的此公共無效方法(此處為測試用例)作為測試用例運行。 下表顯示了 **org.junit.TestCase** 類中可用的一些重要方法: | **S.No.** | **Method** | **Description** | | 1\. | int countTestCases() | 此方法用于計算通過 **run(TestResult tr)**方法執行的測試用例數。 | | 2\. | TestResult createResult() | 此方法用于創建 **TestResult** 對象。 | | 3\. | 字符串 getName() | 此方法返回的字符串不過是 **TestCase** 。 | | 4\. | 運行 TestResult() | 此方法用于執行返回 **TestResult** 對象的測試 | | 5\. | 無效運行(TestResult 結果) | 此方法用于執行具有 **TestResult** 對象的測試,該對象不返回任何內容。 | | 6\. | setName(String name)無效 | 此方法用于設置 **TestCase 的名稱。** | | 7\. | 無效 setUp() | 此方法用于編寫資源關聯代碼。 例如 創建數據庫連接。 | | 8\. | 無效 tearDown() | 此方法用于編寫資源釋放代碼。 例如 執行事務操作后釋放數據庫連接。 | ## JUnit TestResult 類 When you execute a test, it returns a result (in the form of **TestResult** object). This TestResult object can be used to analyse the resultant object. This test result can be either failure or successful. See below table for important methods used in org.junit.TestResult class: | **S.No.** | **Method** | **Description** | | 1\. | void addError(Test test,Throwable t) | 如果您需要在測試中添加錯誤,請使用此方法。 | | 2\. | void addFailure(測試測試,AssertionFailedError t) | 如果需要將故障添加到故障列表中,則使用此方法。 | | 3\. | 無效 endTest(測試測試) | 此方法用于通知已執行測試(完成) | | 4\. | int errorCount() | 此方法用于獲取在測試執行期間檢測到的錯誤。 | | 5\. | 枚舉< TestFailure > errors() | 此方法僅返回錯誤的集合(此處為 Enumeration)。 | | 6\. | int failureCount() | 此方法用于獲取在測試執行期間檢測到的錯誤計數。 | | 7\. | 無效運行(TestCase 測試) | 此方法用于執行測試用例。 | | 8\. | int runCount() | 此方法僅計算執行的測試。 | | 9. | void startTest(測試測試) | 此方法用于通知測試已開始。 | | 10. | 無效 stop() | 此方法用于測試要停止的運行。 | ## JUnit 測試套件類 如果要以指定的順序執行多個測試,可以通過將所有測試合并到一個位置來完成。 這個地方稱為測試套件。 有關 **org.junit.TestSuite** 類中使用的重要方法,請參見下表: | **S.No.** | **Method** | **Description** | | 1\. | void addTest(測試測試) | 如果要向套件添加測試,則使用此方法。 | | 2\. | void addTestSuite(Class <?擴展了 TestCase > testClass) | 如果要在向套件添加測試時指定類,則使用此方法。 | | 3\. | int countTestCases() | 如果要計算測試用例的數量,則使用此方法。 | | 4\. | String getName() | 此方法用于獲取測試套件的名稱。 | | 5\. | void run(TestResult result) | 此方法用于執行測試并在 **TestResult** 對象中收集測試結果。 | | 6\. | void setName(String name) | 此方法用于設置 **TestSuite** 的名稱。 | | 7\. | 測試 testAt(int 索引) | 如果要返回給定索引的測試,則使用此方法。 | | 8\. | int testCount() | 如果要在套件中返回許多測試,則使用此方法。 | | 9\. | 靜態測試警告(字符串消息) | 此方法返回將失敗的測試并記錄警告消息。 | **摘要:** * JUnit 提供了一個可移植的 API,該 API 提供了對編寫單元測試有用的所有重要的類和注釋。 * 在編寫測試用例時非常有用的類 * org.junit.Assert * org.junit.TestCase * org.junit.TestResult * org.junit.TestSuite * list of important and frequently used annotations @之前 @課前 @后 @下課以后 @測試 @忽視
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看