-
Notifications
You must be signed in to change notification settings - Fork 101
Add TestMethodsShouldBeVoid recipe to fix tests not discovered
#791
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2b69748
Add TestMethodsShouldBeVoid recipe to fix test methods with non-void …
timtebeek 2bdbe7a
Add TestMethodsShouldBeVoid to JUnit5BestPractices
timtebeek 23c3519
Polish
timtebeek d98cb42
Suppress warnings on tests for the issue we're fixing
timtebeek f7e2fc2
Apply bot suggestions
timtebeek 05f1b66
Update src/main/java/org/openrewrite/java/testing/cleanup/TestMethods…
timtebeek 52a2ac9
TestFactory very much should have a non-void return type
timtebeek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
src/main/java/org/openrewrite/java/testing/cleanup/TestMethodsShouldBeVoid.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| /* | ||
| * Copyright 2025 the original author or authors. | ||
| * <p> | ||
| * Licensed under the Moderne Source Available License (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * <p> | ||
| * https://docs.moderne.io/licensing/moderne-source-available-license | ||
| * <p> | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.openrewrite.java.testing.cleanup; | ||
|
|
||
| import org.jspecify.annotations.Nullable; | ||
| import org.openrewrite.ExecutionContext; | ||
| import org.openrewrite.Recipe; | ||
| import org.openrewrite.TreeVisitor; | ||
| import org.openrewrite.java.JavaIsoVisitor; | ||
| import org.openrewrite.java.JavaVisitor; | ||
| import org.openrewrite.java.tree.J; | ||
| import org.openrewrite.java.tree.JavaType; | ||
| import org.openrewrite.java.tree.Statement; | ||
| import org.openrewrite.java.tree.TypeUtils; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| public class TestMethodsShouldBeVoid extends Recipe { | ||
|
|
||
| @Override | ||
| public String getDisplayName() { | ||
| return "Test methods should have void return type"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "Test methods annotated with `@Test`, `@ParameterizedTest`, `@RepeatedTest`, `@TestFactory`, `@TestTemplate` " + | ||
| "should have `void` return type. Non-void return types can cause test discovery issues, " + | ||
| "and warnings as of JUnit 5.13+. This recipe changes the return type to `void` and removes `return` statements."; | ||
| } | ||
|
|
||
| @Override | ||
| public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
| return new JavaIsoVisitor<ExecutionContext>() { | ||
| @Override | ||
| public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) { | ||
| J.MethodDeclaration m = super.visitMethodDeclaration(method, ctx); | ||
|
|
||
| // Check if method has a test annotation & body | ||
| if (!hasTestAnnotation(m) || m.getBody() == null) { | ||
| return m; | ||
| } | ||
|
|
||
| // Check if return type is already void | ||
| JavaType.Primitive voidType = JavaType.Primitive.Void; | ||
| if (m.getReturnTypeExpression() == null || TypeUtils.isOfType(m.getReturnTypeExpression().getType(), voidType)) { | ||
| return m; | ||
| } | ||
|
|
||
| // Change return type to void | ||
| m = m.withReturnTypeExpression(new J.Primitive( | ||
| m.getReturnTypeExpression().getId(), | ||
| m.getReturnTypeExpression().getPrefix(), | ||
| m.getReturnTypeExpression().getMarkers(), | ||
| JavaType.Primitive.Void | ||
| )); | ||
|
|
||
| // Update method type | ||
| if (m.getMethodType() != null) { | ||
| m = m.withMethodType(m.getMethodType().withReturnType(voidType)); | ||
| } | ||
|
|
||
| // Remove return statements that are not in nested classes or lambdas | ||
| return m.withBody((J.Block) new RemoveDirectReturns().visitBlock(requireNonNull(m.getBody()), ctx)); | ||
| } | ||
|
|
||
| private boolean hasTestAnnotation(J.MethodDeclaration method) { | ||
| for (J.Annotation annotation : method.getLeadingAnnotations()) { | ||
| if (TypeUtils.isOfClassType(annotation.getType(), "org.junit.Test") || | ||
| TypeUtils.isOfClassType(annotation.getType(), "org.junit.jupiter.api.Test") || | ||
| TypeUtils.isOfClassType(annotation.getType(), "org.junit.jupiter.api.RepeatedTest") || | ||
| TypeUtils.isOfClassType(annotation.getType(), "org.junit.jupiter.api.TestFactory") || | ||
| TypeUtils.isOfClassType(annotation.getType(), "org.junit.jupiter.api.TestTemplate") || | ||
| TypeUtils.isOfClassType(annotation.getType(), "org.junit.jupiter.params.ParameterizedTest")) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| private static class RemoveDirectReturns extends JavaVisitor<ExecutionContext> { | ||
| @Override | ||
| public J visitLambda(J.Lambda lambda, ExecutionContext ctx) { | ||
| return lambda; // Retain nested returns | ||
| } | ||
|
|
||
| @Override | ||
| public J visitNewClass(J.NewClass newClass, ExecutionContext ctx) { | ||
| return newClass; // Retain nested returns | ||
| } | ||
|
|
||
| @Override | ||
| public @Nullable J visitReturn(J.Return retrn, ExecutionContext ctx) { | ||
| return retrn.getExpression() instanceof Statement ? | ||
| // Retain any side effects from expressions in return statements | ||
| retrn.getExpression().withPrefix(retrn.getPrefix()) : | ||
| // Remove any other return statements | ||
| null; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.