I am converting somewhere around 1000 JUnit tests with
@Test(expected=SomeException.class)
public void testIt() throws SomeException {
doSomeStuff();
}
to JUnit Jupiter API's
@Test
void testIt() {
Assertions.assertThrows(SomeException.class, () -> {
doSomeStuff();
});
}
In Intellij, I know there is some "Surround With" magic, Live Templates, or something that can make this process a little more automated?
I could always write a script or something, but I figure there's probably a way to utilize Intellij's built-in functionality to make this easier.
Any ideas?
One way of migration multiple Junit4 tests to Junit5 is OpenRewrite.
It will have multiple predefined recipes for common tasks like Migrating Junit4 to Junit5.
If you have a maven project without spring, you can add the following to your 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>
Quick explanation:
<activeRecipes>
<recipe>org.openrewrite.java.testing.junit5.JUnit5BestPractices</recipe>
</activeRecipes>
This part activates the rewrite junit recipe, which is in the dependency org.openrewrite.recipe:rewrite-testing-frameworks
.
If you are using Spring or Spring-Boot you will need the active recipe org.openrewrite.java.spring.boot2.SpringBoot2JUnit4to5Migration
and the following dependency.
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-spring</artifactId>
<version>4.35.0</version>
</dependency>
If you are using Gradle, you should check out the offical documenation.
Via mvn rewrite:dryRun
you can see the results in a .patch
file.
Via mvn rewrite:run
the migration will be done. If you have a VCS, you can always run without doing a dry run, because you can see the diff in your VCS' diff tool.