Repository navigation
ExpectedExceptionToAssertThrows: declare captured exception with the matcher's exception type - #1129
Open
mohitduhan19 wants to merge 2 commits into
Open
mohitduhan19 wants to merge 2 commits into
mohitduhan19 wants to merge 2 commits into
Conversation
51 of 76 tasks
timtebeek
force-pushed
the
fix-1124-expect-matcher-exception-type
branch
from
November 18, 2026 22:56
d2768a6 to
0d1ae17
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What's changed?
ExpectedExceptionToAssertThrowsalways declared the captured exception asThrowable. Whenexpect(Matcher)is given a matcher whose type argument is a specific exception type (e.g.CustomMatcher<AppError>,TypeSafeMatcher<IllegalStateException>, or a factory returningMatcher<AppError>), the emittedassertThat(exception, matcher)does not compile, becauseMatcherAssert.assertThat(T, Matcher<? super T>)can not accept aMatcher<AppError>for aThrowable.The recipe now resolves the matcher's
org.hamcrest.Matcher<T>type argument by walking the matcher expression's supertypes. WhenTis aThrowablesubtype narrower thanThrowable, it declares the captured exception asTand passesT.classtoassertThrows:Details:
expect(X.class)is also present,X.classis kept whenXis a subtype ofT. IfXis a supertype ofT, it is narrowed toT.class. If the two are unrelated, the recipe falls back to the previous behavior.Tlives in another package. Nested types are referenced asOuter.Inner.Throwable(isA(..),instanceOf(..),BaseMatcher<Throwable>) are unchanged, so existing output is unaffected.What's your motivation?
ExpectedExceptionToAssertThrowsemits uncompilable code whenexpect(Matcher)is given a matcher narrower thanThrowable#1124Anything 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, whereExpectedExceptionwould 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
expect(Class)combined with a narrower matcher, and a matcher type from another package (import added). The existingisA(..)tests cover the unchanged path.ExpectedExceptionToAssertThrowsTestand the rest of thejunit5package pass locally. The three new tests fail without the change.