Skip to main content

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 public to package-private
  • Removing test prefix from method names
  • Changing return type to void
  • Removing unnecessary or specific throws declarations
rewrite.yml
---
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.

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.

  1. 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:
shell
mod build ~/workspace/
  1. If the recipe is not available locally yet, then you can install it once using:
shell
mod config recipes jar install org.openrewrite.recipe:rewrite-testing-frameworks:LATEST
mod config recipes yaml install /path/to/your/rewrite.yml
  1. Run the recipe.
shell
mod run ~/workspace/ --recipe com.github.timtebeek.ChangeTestMethodSignatures