如何自动从 JUnit 4 迁移到 JUnit 5?

2023-11-27

本着这个问题从 JUnit 3 到 JUnit 4,是否有任何正则表达式列表高效地从 junit 4 API 迁移到 junit 5 API,无论代码大小如何?


目前的工具还不是很好,但正在改进:

  • IntelliJ:将大多数注释迁移到 JUnit 5 版本。但是,如果您的测试文件包含,则不会执行任何操作@Rules(例如,ExpectedException)自 v2018.2 起。
  • 容易出错:包含内置重构,可自动迁移各种 JUnit 4 异常测试习惯用法(try/fail/catch/assert、ExpectedExceptions, @Test(expected = …)) to assertThrows,完美增强 IntelliJ。

我建议采取以下步骤:

  1. 将 JUnit 5 工件添加到您的测试依赖项中。
  2. Install容易出错的编译器。
  3. Configure it to produce a patch to migrate to assertThrows, enabling the following checks (the example Maven configuration is below):
    • 尝试失败重构,
    • 预期异常重构,
    • 测试异常重构。
  4. 使用IntelliJ内置重构迁移 JUnit 4 JUnit 5 对应的注释和方法。

我不知道有任何工具可以帮助自动迁移Parameterized测试或其他规则ExpectedException.

下面是一个容易出错的配置示例:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.8.0</version>
      <configuration>
        <compilerId>javac-with-errorprone</compilerId>
        <showWarnings>true</showWarnings>
        <forceJavacCompilerUse>true</forceJavacCompilerUse>
        <source>${java.compiler.source}</source>
        <target>${java.compiler.target}</target>
        <compilerArgs>        
          <arg>-XepPatchChecks:TryFailRefactoring,ExpectedExceptionRefactoring,TestExceptionRefactoring</arg>
          <arg>-XepPatchLocation:${project.basedir}</arg>
        </compilerArgs>
      </configuration>
      <dependencies>
        <dependency>
          <groupId>org.codehaus.plexus</groupId>
          <artifactId>plexus-compiler-javac-errorprone</artifactId>
          <version>2.8.3</version>
        </dependency>
        <dependency>
          <groupId>com.google.errorprone</groupId>
          <artifactId>error_prone_core</artifactId>
          <version>2.3.2</version>
        </dependency>
      </dependencies>
    </plugin>
  </plugins>
</build>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何自动从 JUnit 4 迁移到 JUnit 5? 的相关文章

随机推荐