How to automatically migrate from JUnit 4 to JUnit 5?

前端 未结 1 1398
情话喂你
情话喂你 2020-12-21 01:07

In the spirit of this question from JUnit 3 to JUnit 4, are there any list of regular expressions to efficiently migrate from the junit 4 API to the junit 5 API

1条回答
  •  猫巷女王i
    2020-12-21 01:42

    The tooling at the moment is not great, but improving:

    • IntelliJ: Migrates most annotations to JUnit 5 versions. Hovewer, does not do anything if your test file contains @Rules (e.g., ExpectedException) as of v2018.2.
    • Error Prone: contains built-in refactorings to automatically migrate various JUnit 4 exception-testing idioms (try/fail/catch/assert, ExpectedExceptions, @Test(expected = …)) to assertThrows, perfectly augmenting IntelliJ.

    I recommend the following steps:

    1. Add JUnit 5 artefacts to your test dependencies.
    2. Install Error Prone compiler.
    3. Configure it to produce a patch to migrate to assertThrows, enabling the following checks (the example Maven configuration is below):
      • TryFailRefactoring,
      • ExpectedExceptionRefactoring,
      • TestExceptionRefactoring.
    4. Use IntelliJ built-in refactorings to migrate JUnit 4 annotations and methods to JUnit 5 counterparts.

    I am not aware of any tools that help to automatically migrate Parameterized tests, or rules other than ExpectedException.

    Here is an example Error Prone configuration:

    
      
        
          maven-compiler-plugin
          3.8.0
          
            javac-with-errorprone
            true
            true
            ${java.compiler.source}
            ${java.compiler.target}
                    
              -XepPatchChecks:TryFailRefactoring,ExpectedExceptionRefactoring,TestExceptionRefactoring
              -XepPatchLocation:${project.basedir}
            
          
          
            
              org.codehaus.plexus
              plexus-compiler-javac-errorprone
              2.8.3
            
            
              com.google.errorprone
              error_prone_core
              2.3.2
            
          
        
      
    
    

    0 讨论(0)
提交回复
热议问题