Skip to content

Add ThenThrowCheckedExceptionToRuntimeException recipe#954

Merged
timtebeek merged 5 commits intomainfrom
bmuschko/mockito-thenthrow-checked
Apr 7, 2026
Merged

Add ThenThrowCheckedExceptionToRuntimeException recipe#954
timtebeek merged 5 commits intomainfrom
bmuschko/mockito-thenthrow-checked

Conversation

@bmuschko
Copy link
Copy Markdown
Contributor

@bmuschko bmuschko commented Apr 3, 2026

Summary

  • Adds a new recipe ThenThrowCheckedExceptionToRuntimeException that replaces checked exception class literals in thenThrow() calls with RuntimeException.class when the mocked method does not declare the exception in its throws clause
  • Wires the recipe into Mockito1to3Migration (which chains into 1to4 and 1to5)
  • Handles import cleanup for replaced exception types via maybeRemoveImport

Context: In Mockito 3+, thenThrow() validates that checked exceptions are declared in the mocked method's throws clause. Code like when(mock.method()).thenThrow(Exception.class) that worked in Mockito 1.x fails at runtime with MockitoException: Checked exception is invalid for this method! after migration.

Mockito 3+ validates that checked exceptions passed to `thenThrow()`
are declared in the mocked method's `throws` clause. This recipe
replaces undeclared checked exception class literals with
`RuntimeException.class` and cleans up unused imports.
@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Apr 3, 2026
@bmuschko bmuschko added the enhancement New feature or request label Apr 3, 2026
@bmuschko bmuschko requested a review from timtebeek April 3, 2026 14:45
- Replace manual method name/type checks with MethodMatcher
- Use ListUtils.map instead of boolean changed + manual loop
- Inline local variables and combine nested conditionals
@github-project-automation github-project-automation bot moved this from In Progress to Ready to Review in OpenRewrite Apr 7, 2026
…ationTest

The inline stub classes didn't extend Runner, causing compilation
errors on CI. The real PowerMockRunner is already on the classpath
via classpathFromResources, so the stubs are unnecessary.
@timtebeek timtebeek merged commit c33bba2 into main Apr 7, 2026
1 check passed
@timtebeek timtebeek deleted the bmuschko/mockito-thenthrow-checked branch April 7, 2026 21:51
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Apr 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants