Skip to content

Add TestMethodsShouldBeVoid recipe to fix tests not discovered#791

Merged
timtebeek merged 7 commits intomainfrom
fix-790-test-methods-void-return
Aug 17, 2025
Merged

Add TestMethodsShouldBeVoid recipe to fix tests not discovered#791
timtebeek merged 7 commits intomainfrom
fix-790-test-methods-void-return

Conversation

@timtebeek
Copy link
Copy Markdown
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
mergify bot added a commit to robfrank/linklift that referenced this pull request Sep 2, 2025
…rom 3.15.0 to 3.16.0 [skip ci]

Bumps [org.openrewrite.recipe:rewrite-testing-frameworks](https://github.com/openrewrite/rewrite-testing-frameworks) from 3.15.0 to 3.16.0.
Release notes

*Sourced from [org.openrewrite.recipe:rewrite-testing-frameworks's releases](https://github.com/openrewrite/rewrite-testing-frameworks/releases).*

> 3.16.0
> ------
>
> What's Changed
> --------------
>
> * Use `@ValueSource` instead of `@CsvSource` where there's a single arg by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-testing-frameworks#797](https://github.com/openrewrite/rewrite-testing-frameworks/pull/797)
> * Adapt `@EnabledOnJre`, `@DisabledOnJre`, `@EnabledForJreRange` and `@DisabledForJreRange` for JUnit 6's Java 17 minimum by [`@​Jenson3210`](https://github.com/Jenson3210) in [openrewrite/rewrite-testing-frameworks#795](https://github.com/openrewrite/rewrite-testing-frameworks/pull/795)
>
> **Full Changelog**: <openrewrite/rewrite-testing-frameworks@v3.15.1...v3.16.0>
>
> v3.15.1
> -------
>
> What's Changed
> --------------
>
> * Update with rewrite 8.60.2
> * Add `TestMethodsShouldBeVoid` recipe to fix tests not discovered by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-testing-frameworks#791](https://github.com/openrewrite/rewrite-testing-frameworks/pull/791)
> * Recipes to migrate from JUnit 5 to JUnit 6 by [`@​Jenson3210`](https://github.com/Jenson3210) in [openrewrite/rewrite-testing-frameworks#792](https://github.com/openrewrite/rewrite-testing-frameworks/pull/792)
> * Remove lineSeparator annotation attribute by [`@​Jenson3210`](https://github.com/Jenson3210) in [openrewrite/rewrite-testing-frameworks#793](https://github.com/openrewrite/rewrite-testing-frameworks/pull/793)
> * Use classpath from resource for Refaster by [`@​jevanlingen`](https://github.com/jevanlingen) in [openrewrite/rewrite-testing-frameworks#794](https://github.com/openrewrite/rewrite-testing-frameworks/pull/794)
> * Add `JUnitTryFailToAssertThatThrownBy` by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-testing-frameworks#781](https://github.com/openrewrite/rewrite-testing-frameworks/pull/781)
> * JUnit5BestPractices - Upgrade mockwebserver3-junit5 to 5.1.0 version by [`@​philippe-granet`](https://github.com/philippe-granet) in [openrewrite/rewrite-testing-frameworks#796](https://github.com/openrewrite/rewrite-testing-frameworks/pull/796)
>
> **Full Changelog**: <openrewrite/rewrite-testing-frameworks@v3.15.0...v3.15.1>


Commits

* [`d3f66c7`](openrewrite/rewrite-testing-frameworks@d3f66c7) Update documentation examples
* [`f9e03d5`](openrewrite/rewrite-testing-frameworks@f9e03d5) Adapt `@EnabledOnJre`, `@DisabledOnJre`, `@EnabledForJreRange` and `@Disabled...
* [`2f840cb`](openrewrite/rewrite-testing-frameworks@2f840cb) Use `@ValueSource` instead of `@CsvSource` where there's a single arg ([#797](https://github.com/openrewrite/rewrite-testing-frameworks/issues/797))
* [`46caa79`](openrewrite/rewrite-testing-frameworks@46caa79) JUnit5BestPractices - Upgrade mockwebserver3-junit5 to 5.1.0 version ([#796](https://github.com/openrewrite/rewrite-testing-frameworks/issues/796))
* [`31da639`](openrewrite/rewrite-testing-frameworks@31da639) Add `JUnitTryFailToAssertThatThrownBy` ([#781](https://github.com/openrewrite/rewrite-testing-frameworks/issues/781))
* [`ec45a7a`](openrewrite/rewrite-testing-frameworks@ec45a7a) Use `FindDependency` from rewrite-java-dependencies in `AnyToNullable`
* [`fec110e`](openrewrite/rewrite-testing-frameworks@fec110e) Use classpath from resource for Refaster ([#794](https://github.com/openrewrite/rewrite-testing-frameworks/issues/794))
* [`b5bc87a`](openrewrite/rewrite-testing-frameworks@b5bc87a) Remove lineSeparator annotation attribute ([#793](https://github.com/openrewrite/rewrite-testing-frameworks/issues/793))
* [`a9f1463`](openrewrite/rewrite-testing-frameworks@a9f1463) Recipes to migrate from JUnit 5 to JUnit 6 ([#792](https://github.com/openrewrite/rewrite-testing-frameworks/issues/792))
* [`bf91b26`](openrewrite/rewrite-testing-frameworks@bf91b26) Add `TestMethodsShouldBeVoid` recipe to fix tests not discovered ([#791](https://github.com/openrewrite/rewrite-testing-frameworks/issues/791))
* See full diff in [compare view](openrewrite/rewrite-testing-frameworks@v3.15.0...v3.16.0)
  
[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility\_score?dependency-name=org.openrewrite.recipe:rewrite-testing-frameworks&package-manager=maven&previous-version=3.15.0&new-version=3.16.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)
Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.
[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)
---
Dependabot commands and options
  
You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
- `@dependabot show  ignore conditions` will show all of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
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