<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://javatutorial.net/how-to-use-junit-for-unit-testing](https://javatutorial.net/how-to-use-junit-for-unit-testing) ## 什么是軟件測試,為什么有用? 最簡單的測試軟件是評估其是否滿足所有要求,并檢查與當前狀態相關的任何不良行為和錯誤。 ![java-featured-image](https://img.kancloud.cn/05/3e/053ee0bb59842d92359246c98f815e0c_780x330.jpg) ## 用 Java 測試 Java 有多種可用的測試框架,但是,在本教程中,我們將重點介紹 JUnit,因為它是最受歡迎的框架之一。 ## 關于 JUnit 的一些知識 [JUnit](https://javatutorial.net/how-to-use-junit-for-unit-testing) 是一個 Java 框架,它允許我們編寫和運行自動測試。 隨著框架的最新更新,JUnits 的可讀性大大提高。 ## 設置 JUnit 如果您使用的是通用的 [IDE](https://javatutorial.net/choose-your-java-ide-eclipse-netbeans-and-intellij-idea) ,例如 Eclipse,IntelliJ 或 NetBeans,則默認情況下已安裝 JUnit。 如果您不使用 IDE,而僅使用 Maven 或 Gradle,則需要在`pom.xml`或 gradle 文件中將其安裝。 如果在 Eclipse 中創建 [Maven](https://javatutorial.net/how-to-install-maven-on-windows-linux-and-mac) 項目,然后選擇`archetype-quickstart`并轉到`pom.xml`文件,您將看到 Eclipse 自動將 JUnit 添加為依賴項。 ![JUnit added by Eclipse by default](https://img.kancloud.cn/f1/aa/f1aab200f3455c7e46a53b4c66afd4b5_880x508.jpg) 但是,版本是 3.8.1。 我們不想要那樣,我們想要 JUnit 4 或 JUni5。 ### JUnit 4 因此,讓我們刪除默認版本,然后將其替換為較新的版本,如下所示: ![JUnit 4 pom.xml](https://img.kancloud.cn/77/cc/77cc4c1642432c2c40b597ba0d53b118_870x515.jpg) 但是,在將其添加為依賴項并且無法識別`org.junit`之后,請執行以下操作: 在 Eclipse 中:屬性 -&gt; Java 構建路徑 -&gt; 庫 -&gt; 添加庫 -&gt; JUnit ![Adding JUnit as a library in Eclipse](https://img.kancloud.cn/3e/a0/3ea058090e0877d5fd19f59840dd2ef8_1575x650.jpg) ![JUnit library add](https://img.kancloud.cn/5e/a7/5ea77f5043601713e34d42c6e99f96ef_692x599.jpg) ## 如何使用 JUnit 示例 這是我的簡單程序,我們將在以下程序上運行 JUnit 測試: ![Example program to run JUnit on ](https://img.kancloud.cn/96/12/96127af3758cac030c5a23d7ce0494b3_516x311.jpg) ### 我的測試類 ![Test class example](https://img.kancloud.cn/ef/74/ef74375b5c9d4615a12294e8f7340fc6_673x383.jpg) 好的! 我們只在`exmipeMethod`方法上運行了 2 個測試。 ### 分解 注意,方法`testConversion()`帶有 JUnit `@Test`注釋。 這樣做是為了使單元測試運行程序知道此方法代表單元測試,應該執行。 如果沒有此注釋,則不會將其表示為`Test`類,而且不會由測試運行者運行。 `assertEquals()`是執行實際測試的方法。 您真正關心的是第 13 和 17 行。`assertEquals()`方法有兩個參數,第一個參數是預期結果,第二個參數是實際結果。 您可以看到這是多么有用并且非常容易實現。 重要的是要指出,如果預期結果與實際結果匹配,則什么也不會發生,但是,如果預期結果與實際結果不匹配,則`assertEquals`會引發異常,并且測試方法將停止進一步執行。 您可以使用任意數量的測試方法。 ## 運行一個 JUnit ### 在 Eclipse 中運行 在 Eclipse 中,在 Package Explorer 上,找到 JUnit 測試,右鍵單擊,然后選擇“Run As -&gt; JUnit Test”。 ![Run As Junit Eclipse](https://img.kancloud.cn/7d/eb/7deb0e9a370ceeaf1fd9f1baed05c8e0_924x745.jpg) ### 在 Maven 中運行 如果要使用 Maven 運行 JUnit,則可以使用`mvn test`運行整個測試套件,也可以運行特定的測試:`mvn -Dtest=TestName test`。 ### 在 Gradle 中運行 在 gradle 中,對于整個測試套件:`gradlew test`,對于特定測試:`gradlew -Dtest.single=testName test`。
                  <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>

                              哎呀哎呀视频在线观看