<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] # junit4注解 ~~~ /* * @Test:將一個普通方法修飾為一個測試方法 * @Test(exception=XXX.class) * @Test(time=毫秒) * @BeforeClass:它會在所有的測試方法前被執行,static修飾 * @AfterClass:它會在所有的測試方法后被執行,static修飾 * @Before:它會在每一個測試方法前被執行一次 * @After:它會在每一個測試方法后被執行一次 * @Ignore:省略 * @RunWith:修改運行器org。junit。runner。Runner * * */ ~~~ 其實@Test不僅可以修飾一個普通方法為測試方法,還可以獲取異常或者控制測試方法的執行時間 ## @Test的功能 獲取異常 控制測試代碼執行時間 **獲取異常代碼展示** 1. 獲取異常,對異常的捕獲:@Test(expected=XXX.class) ~~~ package com.duo.util; import static org.junit.Assert.*; import org.junit.Test; public class Anotation { @Test(expected=ArithmeticException.class) public void testDivide(){ assertEquals(4, new Calculate().divide(12, 0)); } } ~~~ 2. 沒有通過@Test(expected=ArithmeticException.class)注解: ~~~ package com.duo.util; import static org.junit.Assert.*; import org.junit.Test; public class Anotation { @Test public void testDivide(){ assertEquals(4, new Calculate().divide(12, 0)); } } ~~~ **控制測試代碼執行時間** 測試方法控制@Test(timeout=毫秒),主要是針對代碼中有循環代碼的測試控制或者超時運行不符合預期的判定 1. 我們使用對一個死循環進行測試: ~~~ package com.duo.util; import static org.junit.Assert.*; import org.junit.Test; public class Anotation { @Test(timeout=2000) public void testWhile(){ while(true){ System.out.println("run forever..."); } } } ~~~ 結果及時運行2秒后系統自動停止運行; 2. 讓當前線程運行2000毫秒,測試代碼運行3000毫秒,符合預期結果 ~~~ package com.duo.util; import static org.junit.Assert.*; import org.junit.Test; public class Anotation { @Test(timeout=3000) public void testReadFile(){ try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } ~~~ 運行結果通過; 也可以通過調整測試時間比線程時間小,測試不符合預期的場景; ## Ignore注解 該注解可以忽略當前的運行的方法,有時候改測試方法沒有實現或者以后再實現 ~~~ package com.duo.util; import static org.junit.Assert.*; import org.junit.Ignore; import org.junit.Test; public class Anotation { @Test(expected=ArithmeticException.class) public void testDivide(){ assertEquals(4, new Calculate().divide(12, 0)); } @Ignore @Test(timeout=2000) public void testWhile(){ while(true){ System.out.println("run forever..."); } } @Test(timeout=3000) public void testReadFile(){ try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } ~~~ ## RunWith 可以修改測試運行器:org.junit.runner.Runner ## 斷言:assert 斷言assert的好多方法可以直接使用,主要是使用了靜態導入:`import static org.junit.Assert.*;`
                  <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>

                              哎呀哎呀视频在线观看