Skip to content

SONARJAVA-6305 Do not raise S8220 in tests where DateTimeException is expected#5613

Open
aurelien-coet-sonarsource wants to merge 3 commits intomasterfrom
ac/SONARJAVA-6305-1
Open

SONARJAVA-6305 Do not raise S8220 in tests where DateTimeException is expected#5613
aurelien-coet-sonarsource wants to merge 3 commits intomasterfrom
ac/SONARJAVA-6305-1

Conversation

@aurelien-coet-sonarsource
Copy link
Copy Markdown
Contributor

No description provided.

@hashicorp-vault-sonar-prod
Copy link
Copy Markdown
Contributor

hashicorp-vault-sonar-prod Bot commented May 8, 2026

SONARJAVA-6305

@aurelien-coet-sonarsource aurelien-coet-sonarsource marked this pull request as ready for review May 8, 2026 12:04
@sonar-review-alpha
Copy link
Copy Markdown
Contributor

sonar-review-alpha Bot commented May 8, 2026

Summary

S8220 false-positive suppression in tests expecting DateTimeException

This PR prevents the S8220 rule (InstantConversionsCheck - warns about unsafe Instant.from() conversions) from raising issues in test code that explicitly expects DateTimeException to be thrown.

What changed:

  1. Refactored isTryCatchFail detection logic from AbstractOneExpectedExceptionRuleUnitTestUtils to enable reuse
  2. Created ExpectedExceptionFilter that suppresses S8220 issues in test contexts where DateTimeException is expected
  3. Integrated the new filter into the analysis pipeline

Patterns recognized:

  • JUnit 4: @Test(expected = DateTimeException.class)
  • TestNG: @Test(expectedExceptions = DateTimeException.class)
  • JUnit 5: assertThrows(), assertThrowsExactly()
  • AssertJ: assertThatThrownBy(), assertThatExceptionOfType(), catchThrowableOfType()
  • Try-catch-fail: Try blocks ending with fail() inside catch blocks expecting DateTimeException
  • Union types in catch clauses

What reviewers should know

Where to start:

  • Review the test file src/test/files/filters/ExpectedExceptionFilter.java first (~151 lines) — it clearly demonstrates all supported scenarios with comments indicating expected behavior (// NoIssue vs // WithIssue)
  • Then examine ExpectedExceptionFilter.java implementation to see how each pattern is detected

Key decisions:

  • The filter treats exact type matches and supertype matches differently: assertThrowsExactly(RuntimeException.class) is treated as "broad" (suppresses issues) but isExactlyInstanceOf(RuntimeException.class) is not (raises issues). This distinction is intentional for precision.
  • The filter only suppresses issues within the test's expected exception scope — code outside the assertion/try-block still reports issues (see junit5AssertThrowsOnlyFiltersExecutable test case)
  • DateTimeException supertypes (RuntimeException, Exception, Throwable) are recognized to handle cases where tests catch the broader type

Non-functional changes to review:

  • Minor formatting fixes in UnitTestUtils.java (indentation alignment, spacing before .equals())
  • Test assertion counts updated from 6→7 filters in PostAnalysisIssueFilterTest

Watch for:

  • Union type handling in catch clauses (multi-type catches with |)
  • AssertJ method chaining — the filter follows chains to locate the executable/code block being tested
  • Edge case: assignments in annotation arguments (e.g., @Test(expected = SomeClass.class))

  • Generate Walkthrough
  • Generate Diagram

🗣️ Give feedback

sonar-review-alpha[bot]

This comment was marked as resolved.

sonar-review-alpha[bot]

This comment was marked as resolved.

@sonarqube-next
Copy link
Copy Markdown

sonarqube-next Bot commented May 8, 2026

Copy link
Copy Markdown
Contributor

@sonar-review-alpha sonar-review-alpha Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! ✅

🗣️ Give feedback

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant