Repository navigation
[java] New rule: AssertEqualsArgumentOrder - #6713
Conversation
7030bf0 to
b1684a9
Compare
|
Compared to main: (comment created at 2026-06-19 17:09:29+00:00 for 8cc3eaa) |
|
You might want to add support for TestNG, spring-test, and JSONAssert |
This comment was marked as resolved.
This comment was marked as resolved.
34fc162 to
fe550f1
Compare
adangel
left a comment
There was a problem hiding this comment.
Thanks!
Since there is no extra issue, I assume, that you added this rule because you have a real need for it...
And since there is no extra issue, can you update the PR description to describe the new rule? Like we require for issues/new-rule-feature-request (-> https://github.com/pmd/pmd/blob/main/.github/ISSUE_TEMPLATE/2new_rule.md ). There is no need to create an extra issue for that, the PR is enough. But having the description correct helps later on for documentation. E.g. I will add this PR in the release notes under "java-errorprone" with a link to this PR and it would be good if the PR description is complete... Thanks!
|
@adangel I checked how other frameworks do this (links in initial comment) and I'm no longer sure this should be a single rule -- the indication is the same (constant being used as "actual"), but the implications are different: in some cases the error message is confusing, in others it's likely a copy-paste error making the whole test method pointless. The upside of having one rule for both is that we don't have to inspect the same nodes twice. Btw. the trivial assertions with |
I'd say the scope of the rule is fine as it is. What you have right now is AFAICT equivalent to MisorderedAssertEqualsArguments/AssertEqualsArgumentOrderChecker, isn't it? The other linked checks could serve as ideas for other rules. |
Well right now it also detects the comparison of two literals, which falls under something like EqualsWithItself. The question is whether we ever want to have a rule for detecting |
|
Ah, I see. Looking at the rule description...
That indeed "feels" like two different things. |
ba6097a to
2324d7b
Compare
In general, I like the rule. It will be useful (and we should enable it for our own dogfood ruleset). I think, we should keep it as one rule - a rule that concentrates on problems around using assertEquals. If we ever do quickfix/autofix (which would require to know which solution is correct), we can split the rule later at anytime.
Maybe we can describe the rule a bit more general like "There is an assert equals statement, that doesn't look right". Thanks to your comments about the parameter order, I remembered now the problem when switching from Junit4 to Junit Jupiter: If - for some reason - you provided a message, but use the same value for the message as expected, then upgrading to JUnit Jupiter by just statically importing the other assertEquals gives you the problem "expected and actual are the same"... I remember, I had to touch every assertEquals, when we migrated to Junit 5 in PMD... So, maybe, we just shouldn't suggest to replace the assertEquals with "fail" or remove it. Maybe we should just point out, that "expected" and "actual" parameters are the same - and this not something you usually want. We could of course check, whether we have 3 parameters and suggest to move the first parameter to the end (assuming this was a JUnit4 message parameter). But I wouldn't do that and keep it simple. But I see, you already updated the rule description... Alternative rule name suggestions: AssertEqualsArgumentMismatch, PotentialAssertEqualsArgumentMismatch, UnusualAssertEquals rule violation message suggestion: assertEquals() called with swapped arguments or constants also the rule example should probably be updated to not only include constants, but also include the swapped expected/actual case. |
adangel
left a comment
There was a problem hiding this comment.
Thanks, looks good.
The rule name is good actually, no need to change.
Co-authored-by: Andreas Dangel <[email protected]>
Proposed Rule Name: AssertEqualsArgumentOrder
Proposed Category: Error Prone
Description:
The new Java rule
AssertEqualsArgumentOrderdetects assertions about constants in tests. This helps find assertions that are producing a confusing error message when they fail.Code Sample:
Possible Properties:
Probably not.
Equivalent rules in other checkers
There are other rules for
assertEqualsthat could be merged with this one, but were kept out of scope for simplicity.Ready?
./mvnw clean verifypasses (checked automatically by github actions)