<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特點 * 使用斷言(Assertion)測試期望結果 * 可以方便的組織和運行測試 * 可以方便的查看測試結果 * 常用的開發工具IDEA, Eclipse都集成了JUnit * 可以方便的集成到maven項目中 ## maven依賴 ``` <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.x</version> </dependency> ``` ## 項目中的目錄結構 項目目錄必須是如下,測試類規定標準是在test目錄中進行測試 ``` ├── src │ ├── main │ │ └── java │ │ └── resources │ └── test │ │ └── java │ │ └── resources ``` ## 注解介紹 * @BeforeClass 全局只會執行一次,而且是第一個運行,用來初始化非常耗時的資源, 例如創建數據庫 * @Before 在測試方法運行之前運行,初始化測試資源 * @Test 測試方法 * @Test(excepted=xx.class): xx.class表示異常類,表示測試的方法拋出此異常時,認為是正常的測試通過的 * @Test(timeout=毫秒數) :測試方法執行時間是否符合預期 * @After 在測試方法運行之后允許,釋放測試資源 * @AfterClass 全局只會執行一次,而且是最后一個運行,清理@BeforeClass創建的資源, 例如創建數據庫 * @Ignore 忽略此方法 * @RunWith:可以更改默認測試運行器org.junit.runner.Runner,JUnit使用Runner運行測試代碼,在JUnit中有很多個 Runner,如果你沒有指定,那么系統自動使用默認 Runner來運行你的代碼。 ## 測試類方法的執行順序 ``` BeforeClass() 測試類構造方法() Before() testA() After() Before() testB() After() AfterClass() ``` ## 斷言介紹 斷言的使用, 必須先引入必須的包,IDE創建的會自動引入 ``` import static org.junit.Assert.*; ``` assertEquals(100, x): 斷言相等 assertArrayEquals({1, 2, 3}, x): 斷言數組相等 assertNull(x): 斷言為null assertTrue(x > 0): 斷言為true assertFalse(x < 0): 斷言為false; assertNotEquals: 斷言不相等 assertNotNull: 斷言不為null ## 參數化測試 * 可能會遇到這種測試,同樣的邏輯方法,輸入不同的參數,期望得到不同的結果,在以前需要寫多個測試方法,JUnit4提供了參數化測試,需要在類上面指定如下的運行器:@RunWith (Parameterized.class) * 在提供數據的方法上加上一個@Parameters注解,這個方法必須是靜態static的,并且返回一個集合Collection,該方法沒有參數。 * 為測試類聲明幾個變量,分別用于存放期望值和測試所用數據。 * 為測試類聲明一個帶有參數的公共構造函數,并在其中為上個環節中聲明的幾個變量賦值。 ``` package org.test; import java.util.Arrays; import java.util.Collection; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; /** * 參數化測試的類必須有Parameterized測試運行器修飾 * */ @RunWith(Parameterized.class) public class AddTest { private int input1; private int input2; private int expected; /** * 準備數據,數據的準備需要在一個方法中進行,該方法需要滿足一定的要求: 1)該方法必須由Parameters注解修飾 2)該方法必須為public static的 3)該方法必須返回Collection類型 4)該方法的名字不做要求 5)該方法沒有參數 */ @Parameters @SuppressWarnings("unchecked") public static Collection prepareData(){ Object [][] object = {{-1,-2,-3},{0,2,2},{-1,1,0},{1,2,3}}; return Arrays.asList(object); } public AddTest(int input1,int input2,int expected){ this.input1 = input1; this.input2 = input2; this.expected = expected; } @Test public void testAdd(){ Add add = new Add(); int result = add.add(input1, input2); Assert.assertEquals(expected,result); } } ```
                  <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>

                              哎呀哎呀视频在线观看