10

I am trying to migrate test files from Junit 4 to Junit 5 using IntelliJ refactor code -> migrate -> junit 4 to junit 5.

But it only run import optimization and show all unit tests in the project and ask yes or no on refactoring and nothing happens when I chose do refactor.

any one know on another migration tool / plug in or a way to make the migration work ?

Stefan Birkner
  • 24,059
  • 12
  • 57
  • 72
Ran Koretzki
  • 464
  • 3
  • 15

3 Answers3

10

Jeanne Boyarsky created a tool to convert JUnit 4 based tests to JUnit Jupiter.

You can find it in her GitHub account: https://github.com/boyarsky/convert-junit4-to-junit5

Sam Brannen
  • 29,611
  • 5
  • 104
  • 136
  • 1
    See also the brilliant built-in Error Prone refactorings to migrate ExpectedExceptions and other exception-testing idioms to `assertThrows`, a rather tedious task if done manually: https://stackoverflow.com/a/51847402/1792188 – Dmitry Timofeev Aug 15 '18 at 18:11
5

Use openrewrite:

mvn org.openrewrite.maven:rewrite-maven-plugin:4.36.0:run -Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-testing-frameworks:1.28.0 -Drewrite.activeRecipes=org.openrewrite.java.testing.junit5.JUnit5BestPractices

which migrates automatically from JUnit 4 to JUnit 5 without modifying your code style.

Geoffrey De Smet
  • 26,223
  • 11
  • 73
  • 120
2

It seems that converting test from JUnit 4 to JUnit 5 in IntelliJ 2017.3 is not complete. As suggested by Sam Brannen there is a small application on Github called convert-junit4-to-junit5 that can do the job.

Just clone the Github repo and build the software using Maven mvn clean package. Then run the the application using

java -jar \
     <path-to-your-cloned-repo>/convert-junit4-to-junit5/target/convert-junit4-to-to-junit5-0.0.1-SNAPSHOT.jar \
     <path to where your JUnit 4 tests are located>

For further instruction please read the projects documentation.

saw303
  • 8,051
  • 7
  • 50
  • 90