Change test method signatures
When it comes to adopting better testing practices for your own projects, you'll likely want to clear out some common old conventions first, before applying further changes. This allows you to have a single focussed set of changes which you can adopt without a need for review, and reduces the chance of conflicts with subsequent changes.
To start, we'll want to change the method signatures of our test methods to follow modern conventions. This includes:
- Changing test method visibility from
publicto package-private - Removing
testprefix from method names - Changing return type to
void - Removing unnecessary or specific
throwsdeclarations
---
type: specs.openrewrite.org/v1beta/recipe
name: com.github.timtebeek.ChangeTestMethodSignatures
displayName: Change test method signatures
description: Change test method visibility, name and exceptions thrown.
preconditions:
- org.openrewrite.java.search.IsLikelyTest
recipeList:
- org.openrewrite.java.testing.cleanup.TestsShouldNotBePublic
- org.openrewrite.java.testing.cleanup.TestMethodsShouldBeVoid
- org.openrewrite.java.testing.cleanup.RemoveTestPrefix
- org.openrewrite.java.testing.cleanup.SimplifyTestThrows
You can run OpenRewrite recipes directly from IntelliJ IDEA Ultimate; after adding the file to your repository, you should see a run icon in the left margin offering to run the recipe.
If you're not using IntelliJ IDEA Ultimate, you can run the above recipe using one of the following methods.
- Moderne CLI
- Maven Command Line
- Maven POM
- Gradle init script
- Gradle
The Moderne CLI allows you to run OpenRewrite recipes on your project without needing to modify your build files, against serialized Lossless Semantic Tree (LST) of your project for a considerable performance boost & across projects.
You will need to have configured the Moderne CLI on your machine before you can run the following command.
- If project serialized Lossless Semantic Tree is not yet available locally, then build the LST. This is only needed the first time, or after extensive changes:
mod build ~/workspace/
- If the recipe is not available locally yet, then you can install it once using:
mod config recipes jar install org.openrewrite.recipe:rewrite-testing-frameworks:LATEST
mod config recipes yaml install /path/to/your/rewrite.yml
- Run the recipe.
mod run ~/workspace/ --recipe com.github.timtebeek.ChangeTestMethodSignatures
You will need to have Maven installed on your machine before you can run the following command.
mvn -U org.openrewrite.maven:rewrite-maven-plugin:run -Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-testing-frameworks:RELEASE -Drewrite.activeRecipes=com.github.timtebeek.ChangeTestMethodSignatures -Drewrite.exportDatatables=true
You may add the plugin to your pom.xml file, so that it is available for all developers and CI/CD pipelines.
- Add the following to your
pom.xmlfile:
<project>
<build>
<plugins>
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>LATEST</version>
<configuration>
<exportDatatables>true</exportDatatables>
<activeRecipes>
<recipe>com.github.timtebeek.ChangeTestMethodSignatures</recipe>
</activeRecipes>
</configuration>
<dependencies>
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-testing-frameworks</artifactId>
<version>LATEST</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
- Run run the recipe.
mvn rewrite:run
Gradle init scripts are a good way to try out a recipe without modifying your build.gradle file.
- Create a file named
init.gradlein the root of your project.
initscript {
repositories {
maven { url "https://plugins.gradle.org/m2" }
}
dependencies { classpath("org.openrewrite:plugin:latest.release") }
}
rootProject {
plugins.apply(org.openrewrite.gradle.RewritePlugin)
dependencies {
rewrite("org.openrewrite.recipe:rewrite-testing-frameworks:latest.release")
}
rewrite {
activeRecipe("com.github.timtebeek.ChangeTestMethodSignatures")
setExportDatatables(true)
}
afterEvaluate {
if (repositories.isEmpty()) {
repositories {
mavenCentral()
}
}
}
}
- Run the recipe.
gradle --init-script init.gradle rewriteRun
You can add the plugin to your build.gradle file, so that it is available for all developers and CI/CD pipelines.
- Add the following to your
build.gradlefile:
plugins {
id("org.openrewrite.rewrite") version("latest.release")
}
rewrite {
activeRecipe("com.github.timtebeek.ChangeTestMethodSignatures")
setExportDatatables(true)
}
repositories {
mavenCentral()
}
dependencies {
rewrite("org.openrewrite.recipe:rewrite-testing-frameworks:latest.release")
}
- Run
gradle rewriteRunto run the recipe.