原文: https://howtodoinjava.com/junit-5-tutorial/

这个 JUnit5 教程讨论了它如何适应 Java8 编码风格以及其他一些功能。 了解它与 JUnit 3 或 4 的区别。

JUnit5 是 Java 应用使用最广泛的测试框架。 长期以来,JUnit 一直在完美地完成其工作。 在这两者之间,JDK 8 带来了 Java 中非常令人兴奋的功能,最著名的是 lambda 表达式。 JUnit5 旨在适应 Java8 的编码风格以及其他一些功能,这就是为什么需要 Java8 在 JUnit5 中创建和执行测试(尽管可以执行用 JUnit 3 或 JUnit4 编写的测试以实现向后兼容) 。

  1. Table of Contents
  2. Architecture
  3. Installation
  4. Annotations
  5. Writing Tests
  6. Test Suites
  7. Assertions
  8. Assumptions
  9. Backward Compatibility
  10. Conclusion

JUnit5 架构

由于与 JUnit4 相比,JUnit5 由来自三个不同子项目的几个不同模块组成:

  1. JUnit5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
  1. JUnit 平台


为了能够启动 junit 测试,IDE,构建工具或插件需要包含和扩展平台 API。 它定义了TestEngine API,用于开发在平台上运行的新测试框架。
它还提供了一个控制台启动器,可以从命令行启动平台并为 Gradle 和 Maven 构建插件。

  1. JUnit Jupiter


它包括用于编写测试的新编程和扩展模型。 它具有所有新的 junit 注解和TestEngine实现,以运行使用这些注解编写的测试。

  1. JUnit Vintage


其主要目的是支持在 JUnit5 平台上运行 JUnit 3 和 JUnit4 书面测试。 它具有向后兼容性。

安装

您可以在 Maven 或 Gradle 项目中使用 JUnit5,方法是至少包含两个依赖项,即 Jupiter 引擎依赖项和平台运行期依赖项。

  1. //pom.xml
  2. <properties>
  3. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  4. <maven.compiler.source>1.8</maven.compiler.source>
  5. <maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
  6. <junit.jupiter.version>5.5.2</junit.jupiter.version>
  7. <junit.platform.version>1.5.2</junit.platform.version>
  8. </properties>
  9. <dependencies>
  10. <dependency>
  11. <groupId>org.junit.jupiter</groupId>
  12. <artifactId>junit-jupiter-engine</artifactId>
  13. <version>${junit.jupiter.version}</version>
  14. <scope>test</scope>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.junit.platform</groupId>
  18. <artifactId>junit-platform-runner</artifactId>
  19. <version>${junit.platform.version}</version>
  20. <scope>test</scope>
  21. </dependency>
  22. </dependencies>
  23. //build.gradle
  24. testRuntime("org.junit.jupiter:junit-jupiter-engine:5.5.2")
  25. testRuntime("org.junit.platform:junit-platform-runner:1.5.2")

阅读更多: Maven 示例 | Gradle 示例

JUnit5 注解

JUnit5 提供了以下注解来编写测试。

注解 描述
@BeforeEach 带注解的方法将在测试类中的每个测试方法之前运行。
@AfterEach 带注解的方法将在测试类中的每个测试方法之后运行。
@BeforeAll 带注解的方法将在测试类中的所有测试方法之前运行。 此方法必须是静态的。
@AfterAll 带注解的方法将在测试类中的所有测试方法之后运行。 此方法必须是静态的。
@Test 用于将方法标记为 junit 测试
@DisplayName 用于为测试类或测试方法提供任何自定义显示名称
@Disable 它用于禁用或忽略测试套件中的测试类或方法。
@Nested 用于创建嵌套的测试类
@Tag 用标签标记测试方法或测试类,以进行测试发现和过滤
@TestFactory 标记方法为动态测试的测试工厂

在 JUnit5 中编写测试

在 JUnit4 和 JUnit5 之间,测试书写样式没有太大变化。 这是使用生命周期方法的示例测试。

  1. import org.junit.jupiter.api.AfterAll;
  2. import org.junit.jupiter.api.AfterEach;
  3. import org.junit.jupiter.api.Assertions;
  4. import org.junit.jupiter.api.BeforeAll;
  5. import org.junit.jupiter.api.BeforeEach;
  6. import org.junit.jupiter.api.Disabled;
  7. import org.junit.jupiter.api.Tag;
  8. import org.junit.jupiter.api.Test;
  9. import com.howtodoinjava.junit5.examples.Calculator;
  10. public class AppTest {
  11. @BeforeAll
  12. static void setup(){
  13. System.out.println("@BeforeAll executed");
  14. }
  15. @BeforeEach
  16. void setupThis(){
  17. System.out.println("@BeforeEach executed");
  18. }
  19. @Tag("DEV")
  20. @Test
  21. void testCalcOne()
  22. {
  23. System.out.println("======TEST ONE EXECUTED=======");
  24. Assertions.assertEquals( 4 , Calculator.add(2, 2));
  25. }
  26. @Tag("PROD")
  27. @Disabled
  28. @Test
  29. void testCalcTwo()
  30. {
  31. System.out.println("======TEST TWO EXECUTED=======");
  32. Assertions.assertEquals( 6 , Calculator.add(2, 4));
  33. }
  34. @AfterEach
  35. void tearThis(){
  36. System.out.println("@AfterEach executed");
  37. }
  38. @AfterAll
  39. static void tear(){
  40. System.out.println("@AfterAll executed");
  41. }
  42. }

测试套件

使用 JUnit5 测试套件,您可以运行分散到多个测试类和不同包中的测试。 JUnit5 提供了两个注解:@SelectPackages@SelectClasses以创建测试套件。

要执行套件,您将使用@RunWith(JUnitPlatform.class)

  1. @RunWith(JUnitPlatform.class)
  2. @SelectPackages("com.howtodoinjava.junit5.examples")
  3. public class JUnit5TestSuiteExample
  4. {
  5. }

此外,您可以使用以下注解来过滤测试包,类甚至测试方法。

  1. @IncludePackages@ExcludePackages过滤包
  2. @IncludeClassNamePatterns@ExcludeClassNamePatterns过滤测试类
  3. @IncludeTags@ExcludeTags过滤测试方法
  1. @RunWith(JUnitPlatform.class)
  2. @SelectPackages("com.howtodoinjava.junit5.examples")
  3. @IncludePackages("com.howtodoinjava.junit5.examples.packageC")
  4. @ExcludeTags("PROD")
  5. public class JUnit5TestSuiteExample
  6. {
  7. }

阅读更多: JUnit5 测试套件

断言

断言有助于通过测试用例的实际输出来验证期望的输出。 为简单起见,所有 JUnit Jupiter 断言都是org.junit.jupiter.Assertions类中的静态方法,例如assertEquals()assertNotEquals()

  1. void testCase()
  2. {
  3. //Test will pass
  4. Assertions.assertNotEquals(3, Calculator.add(2, 2));
  5. //Test will fail
  6. Assertions.assertNotEquals(4, Calculator.add(2, 2), "Calculator.add(2, 2) test failed");
  7. //Test will fail
  8. Supplier<String> messageSupplier = ()-> "Calculator.add(2, 2) test failed";
  9. Assertions.assertNotEquals(4, Calculator.add(2, 2), messageSupplier);
  10. }

阅读更多: JUnit5 断言

假设

Assumptions类提供静态方法来支持基于假设的条件测试执行。 假设失败会导致测试中止。 通常在没有必要继续执行给定测试方法的情况下使用假设。 在测试报告中,这些测试将被标记为通过。

JUnit jupiter Assumptions类具有两个此类方法:assumeFalse()assumeTrue()

  1. public class AppTest {
  2. @Test
  3. void testOnDev()
  4. {
  5. System.setProperty("ENV", "DEV");
  6. Assumptions.assumeTrue("DEV".equals(System.getProperty("ENV")), AppTest::message);
  7. }
  8. @Test
  9. void testOnProd()
  10. {
  11. System.setProperty("ENV", "PROD");
  12. Assumptions.assumeFalse("DEV".equals(System.getProperty("ENV")));
  13. }
  14. private static String message () {
  15. return "TEST Execution Failed :: ";
  16. }
  17. }

阅读更多: JUnit5 假设

JUnit 3 或 JUnit4 的向后兼容性

JUnit4 已经存在了很长时间,并且用 JUnit4 编写了许多测试。JUnitJupiter 也需要支持这些测试。 为此,开发了 JUnit Vintage 子项目。

JUnit Vintage 提供了TestEngine实现,用于在 JUnit5 平台上运行基于 JUnit 3 和 JUnit4 的测试。

总结

JUnit5 仍在开发中。 看起来如此令人兴奋且功能丰富。 现在,它可以通过第三方工具和 API 进行扩展。 作为一名测试作者,您可能不会有太大的不同,但是当您尝试扩展它或尝试开发任何 IDE 插件时,您会称赞它。

作为开发人员,您还可以考虑将测试模板添加到 Eclipse IDE 中以提高开发速度。

在评论中写下您对此 JUnit5 教程的想法。

学习愉快!

源码下载