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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # JUnit5 教程 > 原文: [https://howtodoinjava.com/junit-5-tutorial/](https://howtodoinjava.com/junit-5-tutorial/) 這個 **JUnit5 教程**討論了它如何適應 Java8 編碼風格以及其他一些功能。 了解它與 JUnit 3 或 4 的區別。 JUnit5 是 Java 應用使用最廣泛的測試框架。 長期以來,JUnit 一直在完美地完成其工作。 在這兩者之間,JDK 8 帶來了 Java 中非常令人興奮的功能,最著名的是 [lambda 表達式](//howtodoinjava.com/java8/complete-lambda-expressions-tutorial-in-java/)。 JUnit5 旨在適應 Java8 的編碼風格以及其他一些功能,這就是為什么需要 Java8 在 JUnit5 中創建和執行測試(盡管可以執行用 JUnit 3 或 JUnit4 編寫的測試以實現向后兼容) 。 ```java Table of Contents Architecture Installation Annotations Writing Tests Test Suites Assertions Assumptions Backward Compatibility Conclusion ``` ## JUnit5 架構 由于[與 JUnit4](//howtodoinjava.com/junit-5/junit-5-vs-junit-4/) 相比,JUnit5 由來自三個不同子項目的幾個不同模塊組成: ```java JUnit5 = JUnit Platform + JUnit Jupiter + JUnit Vintage ``` 1. #### JUnit 平臺 為了能夠啟動 junit 測試,IDE,構建工具或插件需要包含和擴展平臺 API。 它定義了`TestEngine` API,用于開發在平臺上運行的新測試框架。 它還提供了一個控制臺啟動器,可以從命令行啟動平臺并為 Gradle 和 Maven 構建插件。 2. #### JUnit Jupiter 它包括用于編寫測試的新編程和擴展模型。 它具有所有新的 junit 注解和`TestEngine`實現,以運行使用這些注解編寫的測試。 3. #### JUnit Vintage 其主要目的是支持在 JUnit5 平臺上運行 JUnit 3 和 JUnit4 書面測試。 它具有向后兼容性。 ## 安裝 您可以在 Maven 或 Gradle 項目中使用 JUnit5,方法是至少包含兩個依賴項,即 Jupiter 引擎依賴項和平臺運行期依賴項。 ```java //pom.xml <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>${maven.compiler.source}</maven.compiler.target> <junit.jupiter.version>5.5.2</junit.jupiter.version> <junit.platform.version>1.5.2</junit.platform.version> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-runner</artifactId> <version>${junit.platform.version}</version> <scope>test</scope> </dependency> </dependencies> //build.gradle testRuntime("org.junit.jupiter:junit-jupiter-engine:5.5.2") testRuntime("org.junit.platform:junit-platform-runner:1.5.2") ``` > 閱讀更多: [Maven 示例](//howtodoinjava.com/junit-5/junit-5-maven-dependency-pom-xml-example/) | [Gradle 示例](//howtodoinjava.com/junit-5/junit-5-gradle-dependency-build-gradle-example/) ## JUnit5 注解 JUnit5 提供了以下注解來編寫測試。 | 注解 | 描述 | | --- | --- | | `@BeforeEach` | 帶注解的方法將在測試類中的每個測試方法之前運行。 | | `@AfterEach` | 帶注解的方法將在測試類中的每個測試方法之后運行。 | | `@BeforeAll` | 帶注解的方法將在測試類中的所有測試方法之前運行。 此方法必須是靜態的。 | | `@AfterAll` | 帶注解的方法將在測試類中的所有測試方法之后運行。 此方法必須是靜態的。 | | `@Test` | 用于將方法標記為 junit 測試 | | `@DisplayName` | 用于為測試類或測試方法提供任何自定義顯示名稱 | | `@Disable` | 它用于禁用或忽略測試套件中的測試類或方法。 | | `@Nested` | 用于創建嵌套的測試類 | | `@Tag` | 用標簽標記測試方法或測試類,以進行測試發現和過濾 | | `@TestFactory` | 標記方法為動態測試的測試工廠 | ## 在 JUnit5 中編寫測試 在 JUnit4 和 JUnit5 之間,測試書寫樣式沒有太大變化。 這是使用[生命周期](//howtodoinjava.com/junit-5/junit-5-test-lifecycle/)方法的示例測試。 ```java import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import com.howtodoinjava.junit5.examples.Calculator; public class AppTest { @BeforeAll static void setup(){ System.out.println("@BeforeAll executed"); } @BeforeEach void setupThis(){ System.out.println("@BeforeEach executed"); } @Tag("DEV") @Test void testCalcOne() { System.out.println("======TEST ONE EXECUTED======="); Assertions.assertEquals( 4 , Calculator.add(2, 2)); } @Tag("PROD") @Disabled @Test void testCalcTwo() { System.out.println("======TEST TWO EXECUTED======="); Assertions.assertEquals( 6 , Calculator.add(2, 4)); } @AfterEach void tearThis(){ System.out.println("@AfterEach executed"); } @AfterAll static void tear(){ System.out.println("@AfterAll executed"); } } ``` ## 測試套件 使用 **JUnit5 測試套件**,您可以運行分散到多個測試類和不同包中的測試。 JUnit5 提供了兩個注解:[`@SelectPackages`](http://junit.org/junit5/docs/current/api/index.html?org/junit/platform/runner/SelectPackages.html)和[`@SelectClasses`](http://junit.org/junit5/docs/current/api/index.html?org/junit/platform/runner/SelectClasses.html)以創建測試套件。 要執行套件,您將使用`@RunWith(JUnitPlatform.class)`。 ```java @RunWith(JUnitPlatform.class) @SelectPackages("com.howtodoinjava.junit5.examples") public class JUnit5TestSuiteExample { } ``` 此外,您可以使用以下注解來過濾測試包,類甚至測試方法。 1. `@IncludePackages`和`@ExcludePackages`過濾包 2. `@IncludeClassNamePatterns`和`@ExcludeClassNamePatterns`過濾測試類 3. `@IncludeTags`和`@ExcludeTags`過濾測試方法 ```java @RunWith(JUnitPlatform.class) @SelectPackages("com.howtodoinjava.junit5.examples") @IncludePackages("com.howtodoinjava.junit5.examples.packageC") @ExcludeTags("PROD") public class JUnit5TestSuiteExample { } ``` > 閱讀更多: [JUnit5 測試套件](//howtodoinjava.com/junit-5/junit5-test-suites-examples/) ## 斷言 斷言有助于通過測試用例的實際輸出來驗證期望的輸出。 為簡單起見,所有 JUnit Jupiter 斷言都是[`org.junit.jupiter.Assertions`](http://junit.org/junit5/docs/current/api/org/junit/jupiter/api/Assertions.html)類中的靜態方法,例如`assertEquals()`,`assertNotEquals()`。 ```java void testCase() { //Test will pass Assertions.assertNotEquals(3, Calculator.add(2, 2)); //Test will fail Assertions.assertNotEquals(4, Calculator.add(2, 2), "Calculator.add(2, 2) test failed"); //Test will fail Supplier<String> messageSupplier = ()-> "Calculator.add(2, 2) test failed"; Assertions.assertNotEquals(4, Calculator.add(2, 2), messageSupplier); } ``` > 閱讀更多: [JUnit5 斷言](//howtodoinjava.com/junit-5/junit-5-assertions-examples/) ## 假設 [`Assumptions`](http://junit.org/junit5/docs/current/api/org/junit/jupiter/api/Assumptions.html)類提供靜態方法來支持基于假設的條件測試執行。 假設失敗會導致測試中止。 通常在沒有必要繼續執行給定測試方法的情況下使用假設。 在測試報告中,這些測試將被標記為通過。 JUnit jupiter `Assumptions`類具有兩個此類方法:`assumeFalse()`和`assumeTrue()`。 ```java public class AppTest { @Test void testOnDev() { System.setProperty("ENV", "DEV"); Assumptions.assumeTrue("DEV".equals(System.getProperty("ENV")), AppTest::message); } @Test void testOnProd() { System.setProperty("ENV", "PROD"); Assumptions.assumeFalse("DEV".equals(System.getProperty("ENV"))); } private static String message () { return "TEST Execution Failed :: "; } } ``` > 閱讀更多: [JUnit5 假設](//howtodoinjava.com/junit-5/junit-5-assumptions-examples/) ## JUnit 3 或 JUnit4 的向后兼容性 JUnit4 已經存在了很長時間,并且用 JUnit4 編寫了許多測試。JUnitJupiter 也需要支持這些測試。 為此,開發了 *JUnit Vintage* 子項目。 JUnit Vintage 提供了`TestEngine`實現,用于在 JUnit5 平臺上運行基于 JUnit 3 和 JUnit4 的測試。 ## 總結 JUnit5 仍在開發中。 看起來如此令人興奮且功能豐富。 現在,它可以通過第三方工具和 API 進行擴展。 作為一名測試作者,您可能不會有太大的不同,但是當您嘗試擴展它或嘗試開發任何 IDE 插件時,您會稱贊它。 作為開發人員,您還可以考慮[將測試模板添加到 Eclipse](//howtodoinjava.com/junit-5/code-test-templates-eclipse/) IDE 中以提高開發速度。 在評論中寫下您對此 JUnit5 教程的想法。 學習愉快! [源碼下載](https://github.com/lokeshgupta1981/Junit5Examples/tree/master/JUnit5Examples)
                  <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>

                              哎呀哎呀视频在线观看