Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* 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.InMemoryExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.Tree;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.Space;
import org.openrewrite.java.tree.TypeUtils;

import java.util.ArrayList;
import java.util.List;

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, " +
"especially in 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
if (!hasTestAnnotation(m)) {
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() != null ? m.getReturnTypeExpression().getId() : Tree.randomId(),
m.getReturnTypeExpression() != null ? m.getReturnTypeExpression().getPrefix() : Space.EMPTY,
m.getReturnTypeExpression() != null ? m.getReturnTypeExpression().getMarkers() : org.openrewrite.marker.Markers.EMPTY,
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
m = m.withBody(removeReturnStatements(m.getBody()));

return m;
Comment thread
timtebeek marked this conversation as resolved.
Outdated
Comment thread
timtebeek marked this conversation as resolved.
Outdated
}

private boolean hasTestAnnotation(J.MethodDeclaration method) {
for (J.Annotation annotation : method.getLeadingAnnotations()) {
if (TypeUtils.isOfClassType(annotation.getType(), "org.junit.jupiter.api.Test") ||
TypeUtils.isOfClassType(annotation.getType(), "org.junit.jupiter.params.ParameterizedTest") ||
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.Test") ||
TypeUtils.isOfClassType(annotation.getType(), "org.testng.annotations.Test")) {
return true;
}
}
return false;
}

private J.@Nullable Block removeReturnStatements(J.@Nullable Block block) {
if (block == null) {
return null;
}
return new RemoveReturnsVisitor().visitBlock(block, new InMemoryExecutionContext());
}
};
}

private static class RemoveReturnsVisitor extends JavaIsoVisitor<ExecutionContext> {
private int nestedLevel = 0;

@Override
public J.Lambda visitLambda(J.Lambda lambda, ExecutionContext ctx) {
nestedLevel++;
J.Lambda result = super.visitLambda(lambda, ctx);
nestedLevel--;
return result;
}

@Override
public J.NewClass visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
if (newClass.getBody() != null) {
nestedLevel++;
J.NewClass result = super.visitNewClass(newClass, ctx);
nestedLevel--;
return result;
}
return super.visitNewClass(newClass, ctx);
}

@Override
public J.Block visitBlock(J.Block block, ExecutionContext ctx) {
J.Block b = super.visitBlock(block, ctx);
if (nestedLevel == 0) {
// Process the statements to remove returns at the top level
List<org.openrewrite.java.tree.Statement> newStatements = new ArrayList<>();
for (org.openrewrite.java.tree.Statement statement : b.getStatements()) {
if (statement instanceof J.Return) {
J.Return return_ = (J.Return) statement;
// If the return has an expression that's not a literal, keep it as a statement
if (return_.getExpression() != null && !(return_.getExpression() instanceof J.Literal)) {
org.openrewrite.java.tree.Expression expr = return_.getExpression();
// Check if the expression is already a statement (e.g., method invocation)
if (expr instanceof org.openrewrite.java.tree.Statement) {
newStatements.add(((org.openrewrite.java.tree.Statement) expr).withPrefix(statement.getPrefix()));
}
// Otherwise, we can't simply convert it - just remove the return
}
// Otherwise, don't add anything (removes "return;" or "return literal;")
} else {
newStatements.add(statement);
}
}
return b.withStatements(newStatements);
}
return b;
}
}
}
26 changes: 26 additions & 0 deletions src/main/resources/META-INF/rewrite/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,32 @@ examples:
language: java
---
type: specs.openrewrite.org/v1beta/example
recipeName: org.openrewrite.java.testing.cleanup.TestMethodsShouldBeVoid
examples:
- description: ''
sources:
- before: |
import org.junit.jupiter.api.Test;

class ATest {
@Test
int testMethod() {
int i = 42;
return i;
}
}
after: |
import org.junit.jupiter.api.Test;

class ATest {
@Test
void testMethod() {
int i = 42;
}
}
language: java
---
type: specs.openrewrite.org/v1beta/example
recipeName: org.openrewrite.java.testing.datafaker.JavaFakerToDataFaker
examples:
- description: ''
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/META-INF/rewrite/junit5.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ recipeList:
- org.openrewrite.java.testing.cleanup.RemoveTestPrefix
- org.openrewrite.java.testing.cleanup.SimplifyTestThrows
- org.openrewrite.java.testing.cleanup.TestsShouldNotBePublic
- org.openrewrite.java.testing.cleanup.TestMethodsShouldBeVoid
- org.openrewrite.java.testing.junit5.AddParameterizedTestAnnotation
- org.openrewrite.java.testing.junit5.RemoveDuplicateTestTemplates
- org.openrewrite.java.testing.junit5.RemoveTryCatchFailBlocks
Expand Down
Loading