Skip to content

ExpectedExceptionToAssertThrows: declare captured exception with the matcher's exception type - #1129

Open
mohitduhan19 wants to merge 2 commits into
openrewrite:mainfrom
mohitduhan19:fix-1124-expect-matcher-exception-type
Open

mohitduhan19 wants to merge 2 commits into
openrewrite:mainfrom
mohitduhan19:fix-1124-expect-matcher-exception-type

Conversation

@mohitduhan19

@mohitduhan19 mohitduhan19 commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

What's changed?

ExpectedExceptionToAssertThrows always declared the captured exception as Throwable. When expect(Matcher) is given a matcher whose type argument is a specific exception type (e.g. CustomMatcher<AppError>, TypeSafeMatcher<IllegalStateException>, or a factory returning Matcher<AppError>), the emitted assertThat(exception, matcher) does not compile, because MatcherAssert.assertThat(T, Matcher<? super T>) can not accept a Matcher<AppError> for a Throwable.

The recipe now resolves the matcher's org.hamcrest.Matcher<T> type argument by walking the matcher expression's supertypes. When T is a Throwable subtype narrower than Throwable, it declares the captured exception as T and passes T.class to assertThrows:

AppError exception = assertThrows(AppError.class, () -> {
    throw new AppError(42);
});
assertThat(exception, new AppErrorMatcher(42));

Details:

  • If expect(X.class) is also present, X.class is kept when X is a subtype of T. If X is a supertype of T, it is narrowed to T.class. If the two are unrelated, the recipe falls back to the previous behavior.
  • An import is added when T lives in another package. Nested types are referenced as Outer.Inner.
  • Matchers whose type argument is free or exactly Throwable (isA(..), instanceOf(..), BaseMatcher<Throwable>) are unchanged, so existing output is unaffected.

What's your motivation?

Anything in particular you'd like reviewers to focus on?

As the issue notes, assertThrows(AppError.class, ..) reports a different exception type as a type mismatch, where ExpectedException would have run the matcher. Both fail the test, so this seemed the better trade-off than leaving the construct unmigrated. Happy to change it if you'd prefer otherwise.

Checklist

  • Added tests for the narrowed cases: the reproduction from the issue, expect(Class) combined with a narrower matcher, and a matcher type from another package (import added). The existing isA(..) tests cover the unchanged path.
  • ExpectedExceptionToAssertThrowsTest and the rest of the junit5 package pass locally. The three new tests fail without the change.

@mohitduhan19 mohitduhan19 changed the title Fix 1124 expect matcher exception type ExpectedExceptionToAssertThrows: declare captured exception with the matcher's exception type Sep 28, 2026
@timtebeek
timtebeek force-pushed the fix-1124-expect-matcher-exception-type branch from d2768a6 to 0d1ae17 Compare November 18, 2026 22:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

ExpectedExceptionToAssertThrows emits uncompilable code when expect(Matcher) is given a matcher narrower than Throwable

1 participant