多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 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)