Skip to content

Conversation

timtebeek
Copy link
Member

@timtebeek timtebeek commented Aug 17, 2025

Summary

  • Implements recipe to fix test methods with non-void return types
  • Changes return type to void for test methods annotated with @Test, @ParameterizedTest, @RepeatedTest, etc.
  • Removes return statements from method bodies while preserving nested lambda/class returns

Details

Some test methods with non-void return types were not being discovered or run correctly, causing broken test coverage and warnings in JUnit 5.13+. This recipe automatically fixes test method signatures to ensure proper test discovery and execution.

Changes made:

  1. Created TestMethodsShouldBeVoid recipe that:

    • Detects test methods with non-void return types
    • Changes the return type to void
    • Removes return statements from the method body
    • Preserves return statements in nested lambdas and anonymous classes
  2. Added comprehensive test coverage with 10 test cases covering:

    • Simple return type changes
    • Return statement removal
    • Preservation of returns in lambdas
    • Preservation of returns in anonymous classes
    • Parameterized tests
    • Repeated tests
    • Multiple return statements
    • Method invocation preservation

Example transformation:

// Before
@Test
int testMethod() {
    int i = 42;
    return i;
}

// After
@Test
void testMethod() {
    int i = 42;
}

Test plan

  • All 10 unit tests pass
  • Recipe correctly handles various test annotations
  • Return statements in nested contexts are preserved
  • Method invocations are preserved when removing returns

Fixes #790

🤖 Generated with Claude Code

…return types

Fixes #790

Test methods annotated with @test, @ParameterizedTest, @RepeatedTest, etc. should have void return type to ensure proper test discovery in JUnit 5.13+. This recipe:
- Changes the return type to void
- Removes return statements from the method body
- Preserves return statements in nested lambdas and anonymous classes

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Aug 17, 2025
- Added recipe to JUnit5BestPractices recipe list
- Added example demonstrating the recipe transformation

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@timtebeek timtebeek self-assigned this Aug 17, 2025
@timtebeek timtebeek marked this pull request as ready for review August 17, 2025 10:35
timtebeek and others added 2 commits August 17, 2025 12:37
…ShouldBeVoid.java

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@timtebeek timtebeek changed the title Add TestMethodsShouldBeVoid recipe to fix test methods with non-void return types Add TestMethodsShouldBeVoid recipe to fix tests not discovered Aug 17, 2025
@timtebeek timtebeek added recipe Recipe request junit labels Aug 17, 2025
@timtebeek timtebeek merged commit bf91b26 into main Aug 17, 2025
2 checks passed
@timtebeek timtebeek deleted the fix-790-test-methods-void-return branch August 17, 2025 11:35
@github-project-automation github-project-automation bot moved this from In Progress to Done in OpenRewrite Aug 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
junit recipe Recipe request
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Recipe to discover test methods with a non-void return type
1 participant