如何为`Assertions.assertThrows`创建一个Live Template?

4

我正在转换大约1000个JUnit测试

@Test(expected=SomeException.class)
public void testIt() throws SomeException {
  doSomeStuff();
}

到JUnit Jupiter API的

@Test
void testIt() {
  Assertions.assertThrows(SomeException.class, () -> {
    doSomeStuff();    
  });
}

在Intellij中,我知道有一些“Surround With”魔法、Live Templates或其他可以使这个过程更自动化的东西?
我可以写一个脚本或其他什么东西,但我想可能有一种方法可以利用Intellij的内置功能来使这个过程更容易。
有什么想法吗?
1个回答

3

将多个Junit4测试迁移到Junit5的一种方法是使用OpenRewrite

它将具有多个预定义的配方,用于常见任务,例如将Junit4迁移到Junit5

如果您有一个没有Spring的Maven项目,则可以将以下内容添加到您的pom.xml中。

<build>
  <plugins>
    <plugin>
      <groupId>org.openrewrite.maven</groupId>
      <artifactId>rewrite-maven-plugin</artifactId>
      <version>4.44.0</version>
      <configuration>
        <activeRecipes>
            <recipe>org.openrewrite.java.testing.junit5.JUnit5BestPractices</recipe>
        </activeRecipes>
      </configuration>
      <dependencies>
        <dependency>
          <groupId>org.openrewrite.recipe</groupId>
          <artifactId>rewrite-testing-frameworks</artifactId>
          <version>1.36.0</version>
        </dependency>
      </dependencies>
    </plugin>
  </plugins>
</build>

简要说明:

<activeRecipes>
   <recipe>org.openrewrite.java.testing.junit5.JUnit5BestPractices</recipe>
</activeRecipes>

这部分激活了重写JUnit配方,该配方位于依赖项org.openrewrite.recipe:rewrite-testing-frameworks中。

如果您正在使用Spring或Spring-Boot,则需要激活配方org.openrewrite.java.spring.boot2.SpringBoot2JUnit4to5Migration和以下依赖项。

<dependency>
  <groupId>org.openrewrite.recipe</groupId>
  <artifactId>rewrite-spring</artifactId>
  <version>4.35.0</version>
</dependency>

如果您正在使用Gradle,您应该查看官方文档

通过mvn rewrite:dryRun,您可以在一个.patch文件中看到结果。

通过mvn rewrite:run进行迁移。如果您有VCS,您可以随时运行而不进行干练,因为您可以在VCS的差异工具中看到差异。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接