Skip to content

Add AssertTrueInsteadOfDedicatedAssertOperation (java:S5785)#160

Merged
RadikalJin merged 1 commit into
mainfrom
feature/s5785-assert-dedicated-assertion
Jun 15, 2026
Merged

Add AssertTrueInsteadOfDedicatedAssertOperation (java:S5785)#160
RadikalJin merged 1 commit into
mainfrom
feature/s5785-assert-dedicated-assertion

Conversation

@RadikalJin

Copy link
Copy Markdown
Member

Implements SonarQube rule java:S5785 "JUnit assertTrue/assertFalse should be simplified to its dedicated assertion". Mirrors the SonarJava detection logic:

  • assertTrue(a == null) → assertNull(a)
  • assertTrue(a != null) → assertNotNull(a)
  • assertTrue(a == b) [prim] → assertEquals(a, b)
  • assertTrue(a != b) [prim] → assertNotEquals(a, b)
  • assertTrue(a == b) [obj] → assertSame(a, b)
  • assertTrue(a != b) [obj] → assertNotSame(a, b)
  • assertTrue(a.equals(b)) → assertEquals(a, b)
  • assertTrue(Objects.equals(a,b)) → assertEquals(a, b)
  • assertTrue(!expr) → complement of assertTrue(expr)
  • assertFalse(...) → complement of the above

Supports JUnit 4 (org.junit.Assert, junit.framework.Assert/TestCase) and JUnit 5 (org.junit.jupiter.api.Assertions), with message argument preserved in its original position (first for JUnit 4, last for JUnit 5). Static wildcard imports are handled without any additional import management.

Four test cases: JUnit 4 qualified calls, JUnit 5 qualified calls (via .txt source file), wildcard static imports, and noop cases.

Implements SonarQube rule java:S5785 "JUnit assertTrue/assertFalse should be
simplified to its dedicated assertion". Mirrors the SonarJava detection logic:

- assertTrue(a == null)       → assertNull(a)
- assertTrue(a != null)       → assertNotNull(a)
- assertTrue(a == b) [prim]   → assertEquals(a, b)
- assertTrue(a != b) [prim]   → assertNotEquals(a, b)
- assertTrue(a == b) [obj]    → assertSame(a, b)
- assertTrue(a != b) [obj]    → assertNotSame(a, b)
- assertTrue(a.equals(b))     → assertEquals(a, b)
- assertTrue(Objects.equals(a,b)) → assertEquals(a, b)
- assertTrue(!expr)            → complement of assertTrue(expr)
- assertFalse(...)             → complement of the above

Supports JUnit 4 (org.junit.Assert, junit.framework.Assert/TestCase) and
JUnit 5 (org.junit.jupiter.api.Assertions), with message argument preserved
in its original position (first for JUnit 4, last for JUnit 5). Static
wildcard imports are handled without any additional import management.

Four test cases: JUnit 4 qualified calls, JUnit 5 qualified calls (via .txt
source file), wildcard static imports, and noop cases.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@RadikalJin RadikalJin merged commit f578a0e into main Jun 15, 2026
@RadikalJin RadikalJin deleted the feature/s5785-assert-dedicated-assertion branch June 15, 2026 15:27
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