[Misc] Fix 30 SonarCloud java:S5785 issues by using dedicated JUnit assertions - #389
[Misc] Fix 30 SonarCloud java:S5785 issues by using dedicated JUnit assertions#389claude[bot] wants to merge 1 commit into
Conversation
…ssertions * Replace `assertTrue(a.equals(b))`/`assertFalse(a.equals(b))` with `assertEquals`/`assertNotEquals` * Replace `assertFalse(a == b)` with `assertNotSame`, keeping the original operand order Co-Authored-By: Vincent Massol <vincent@massol.net>
michitux
left a comment
There was a problem hiding this comment.
This feels wrong, these tests explicitly test the equals methods and using dedicated JUnit assertions might not call them in the ways that the test wanted to call them, or at the very least it is not obvious anymore that they are called correctly. For example, when comparing to null, nobody assures us that null will actually be passed to equals and assertNotEquals doesn't just always return false when one argument is null and the other is non-null.
|
Thanks for the review — you're right that this is the wrong change to make here, and I'm closing the PR. One factual clarification for the record, because it's not obvious from the call site (which is if (a == null) {
return b == null;
}
return a.equals(b);so But that guarantee lives in JUnit's internals rather than in the test, which is your actual Since all 30 sites in this PR are in dedicated The two sibling PRs from the same sweep are unrelated to this concern (comment cleanup, Generated by Claude Code |
* [Misc] Remove useless assignments to local variables (SonarQube java:S1854) * Dead stores reported by SonarCloud; side-effecting calls kept as plain statements. Co-Authored-By: Kimi Code <noreply@moonshot.cn> * [Misc] Replace string concatenations with text blocks in encoder tests (SonarQube java:S6126) Co-Authored-By: Kimi Code <noreply@moonshot.cn> * [Misc] Fix expected/actual argument order in assertions (SonarQube java:S3415) Co-Authored-By: Kimi Code <noreply@moonshot.cn> * [Misc] Use assertEquals/assertNotEquals instead of assertTrue/assertFalse with equals() (SonarQube java:S5785) Co-Authored-By: Kimi Code <noreply@moonshot.cn> * [Misc] Use try-with-resources where semantics are preserved (SonarQube java:S2093) * AbstractFileLoggerTail: extracted the read block into a private helper holding the try-with-resources (checkstyle NestedTryDepth); close order and error path unchanged. Co-Authored-By: Kimi Code <noreply@moonshot.cn> --------- Co-authored-by: Kimi Code <noreply@moonshot.cn>
|
Superseded by #390, which is the better way to close this out. Rather than leaving these issues merely "accepted" in SonarCloud — where the reasoning is invisible to Generated by Claude Code |
Fixes 30 SonarCloud java:S5785
issues in xwiki-rendering — "use the dedicated JUnit assertion instead of a boolean one".
Change
assertTrue(a.equals(b))→assertEquals(a, b)assertFalse(a.equals(b))→assertNotEquals(a, b)assertTrue(a.hashCode() == b.hashCode())→assertEquals(a.hashCode(), b.hashCode())assertFalse(a == b)→assertNotSame(a, b)Operand order is deliberately preserved (the original receiver stays first): JUnit's
assertEquals(expected, actual)callsexpected.equals(actual), so keeping the receiver firstreproduces the original call exactly — which matters for the
equals(null)/equals("other class")contract assertions in these tests.Static imports were adjusted accordingly (
assertTrue/assertFalseremoved only where the file nolonger uses them).
Notes
mvn clean install -Plegacy,quality(Checkstyle, Revapi, JaCoCo and the unit tests)on the 3 modified modules — the converted assertions are exactly the tests that run.
Related
Same sweep in the sibling repos:
Generated by Claude Code