Skip to content

Move AssertThatThrownByAsInstanceOfThrowable after HasMessage variants#2091

Open
knutwannheden wants to merge 1 commit into
PicnicSupermarket:masterfrom
knutwannheden:fix/reorder-assertj-throwing-callable-rules
Open

Move AssertThatThrownByAsInstanceOfThrowable after HasMessage variants#2091
knutwannheden wants to merge 1 commit into
PicnicSupermarket:masterfrom
knutwannheden:fix/reorder-assertj-throwing-callable-rules

Conversation

@knutwannheden

@knutwannheden knutwannheden commented Jan 31, 2026

Copy link
Copy Markdown

The AssertThatThrownByAsInstanceOfThrowable rule matches assertThatExceptionOfType(X).isThrownBy(lambda) and converts it to assertThatThrownBy(lambda).asInstanceOf(throwable(X)). When the original code chains .withMessage(msg) after .isThrownBy(), this shorter rule can match the inner expression first, leaving .withMessage() on the resulting AbstractThrowableAssert type which does not have that method.

By placing this rule after the more specific HasMessage, HasMessageParameters, HasMessageStartingWith, HasMessageContaining, and HasMessageNotContaining variants, those longer templates get a chance to match first when a message assertion is chained.

The AssertThatThrownByAsInstanceOfThrowable rule matches `assertThatExceptionOfType(X).isThrownBy(lambda)` and converts it to `assertThatThrownBy(lambda).asInstanceOf(throwable(X))`. When the original code chains `.withMessage(msg)` after `.isThrownBy()`, this shorter rule can match the inner expression first, leaving `.withMessage()` on the resulting `AbstractThrowableAssert` type which does not have that method.

By placing this rule after the more specific `HasMessage`, `HasMessageParameters`, `HasMessageStartingWith`, `HasMessageContaining`, and `HasMessageNotContaining` variants, those longer templates get a chance to match first when a message assertion is chained.
@coderabbitai

coderabbitai Bot commented Jan 31, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

A nested rule for AssertJ throwable assertions is repositioned within its parent file, with an explanatory comment added to clarify the ordering rationale. Template implementations and functionality remain unchanged.

Changes

Cohort / File(s) Summary
Rule Reordering
error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssertJThrowingCallableRules.java
Moved AssertThatThrownByAsInstanceOfThrowable<T extends Throwable> nested rule to a different position within the file and added a clarifying NB comment regarding the ordering decision. No functional changes to the rule implementations.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: moving the AssertThatThrownByAsInstanceOfThrowable rule after HasMessage variants, which aligns with the primary purpose of the PR.
Description check ✅ Passed The PR description accurately explains the rationale for reordering rules to prevent premature matching that leaves incompatible methods on the result type.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant