Skip to content

Latest commit

 

History

History
108 lines (97 loc) · 4.22 KB

README.md

File metadata and controls

108 lines (97 loc) · 4.22 KB

refaster-junit

A collection of Refaster templates to migrate to JUnit 5. It is superseded by built-in Error Prone refactorings, see example configuration below.

These templates augment the IDEA-built-in refactorings which do not work if a test class contains ExpectedException rule (as of v2018.2).

Currently this project includes a set of rules to migrate from ExpectedException to assertThrows.

Use built-in Error Prone refactorings

<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>
        <!-- 2.3.2 is not live at the moment! -->
        <version>2.3.2-SNAPSHOT</version>
      </dependency>
    </dependencies>
  </plugin>
</plugins>

For up-to-date info, visit http://errorprone.info/bugpatterns

How to use

Prerequisites

  1. JDK 9+.
  2. Maven 3.5+.

Build a rule

Clone this repository and run build-rule.sh script from the root directory to compile a rule. For example, to build the rule processing ExpectedExceptions:

./build-rule.sh src/main/java/dt/refaster/junit/ExpectedExceptionRule.java expected-exception.refaster

Patch the target project

Use the output Refaster rule file to produce a patch for your project. You will need to enable Error Prone compiler and configure it to use your Refaster rule:

<plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <compilerId>javac-with-errorprone</compilerId>
          <forceJavacCompilerUse>true</forceJavacCompilerUse>
          <source>8</source>
          <target>8</target>
          <compilerArgs>
            <!-- Enter the full path to the compiled Refaster rule below: -->
            <arg>-XepPatchChecks:refaster:/full/path/to/output-rule-name.refaster</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.1</version>
          </dependency>
        </dependencies>
      </plugin>
</plugins>

A complete Maven example can be found in the error prone repository. Please note that it inherits most of 'maven-compiler-plugin' configuration from its parent.

Migrate other APIs

Remove the ExpectedException rules from your test classes and then run the IDEA built-in refactoring to migrate JUnit 4 annotations and methods to JUnit 5 counterparts.