GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] New rule: AssertEqualsArgumentOrder by zbynek · Pull Request #6713 · pmd/pmd

Conversation

@zbynek

@zbynek zbynek commented May 25, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Proposed Rule Name: AssertEqualsArgumentOrder

Proposed Category: Error Prone

Description:

The new Java rule AssertEqualsArgumentOrder detects assertions about constants in tests. This helps find assertions that are producing a confusing error message when they fail.

Code Sample:

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class SimpleTest {
    @Test
    public void testX() {
        assertEquals(next("foo"), "bar"); // wrong argument order
        assertEquals("bar", next("foo")); // good
    }

    public String next(String arg) {
        return "bar";
    }
}

Possible Properties:

  • Should this rule be customizable via properties?

Probably not.

Equivalent rules in other checkers

  • MisorderedAssertEqualsArguments IntelliJ
  • AssertEqualsArgumentOrderChecker in Error Prone

There are other rules for assertEquals that could be merged with this one, but were kept out of scope for simplicity.

  • EqualsWithItself in IntelliJ
  • SelfAssertion in Error Prone
  • ConstantAssertArgument in IntelliJ

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@zbynek
zbynek force-pushed the const-assert branch 2 times, most recently from 7030bf0 to b1684a9 Compare May 25, 2026 22:36
@pmd-actions-helper

pmd-actions-helper Bot commented May 25, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.

Regression Tester Report

(comment created at 2026-06-19 17:09:29+00:00 for 8cc3eaa)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@UncleOwen

UncleOwen commented May 26, 2026

Copy link
Copy Markdown
Member

You might want to add support for TestNG, spring-test, and JSONAssert

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@adangel adangel added the a:new-rule Proposal to add a new built-in rule label May 26, 2026
@adangel

This comment was marked as resolved.

Sign in to view
@zbynek
zbynek force-pushed the const-assert branch 4 times, most recently from 34fc162 to fe550f1 Compare June 10, 2026 00:10
@zbynek
zbynek marked this pull request as ready for review June 10, 2026 00:12
@zbynek
zbynek force-pushed the const-assert branch from fe550f1 to f41bf5e Compare June 10, 2026 20:20
@adangel adangel changed the title [java] new rule ConstantInEqualityAssertion for assertEquals [java] New rule: ConstantInEqualityAssertion for assertEquals Jun 11, 2026
adangel
adangel reviewed Jun 11, 2026
View reviewed changes

@adangel adangel left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

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!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread ...main/java/net/sourceforge/pmd/lang/java/rule/errorprone/ConstantInEqualityAssertionRule.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-java/src/main/resources/category/java/errorprone.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-java/src/main/resources/category/java/errorprone.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-java/src/main/resources/category/java/errorprone.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread ...main/java/net/sourceforge/pmd/lang/java/rule/errorprone/ConstantInEqualityAssertionRule.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@zbynek
zbynek marked this pull request as draft June 11, 2026 07:36
@zbynek
zbynek force-pushed the const-assert branch from 39fa4b4 to 5a53c2c Compare June 11, 2026 22:15
@zbynek
zbynek marked this pull request as ready for review June 11, 2026 22:16
@zbynek

zbynek commented Jun 11, 2026

Copy link
Copy Markdown
Contributor Author

@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 assertTrue are covered by UnnecessaryBooleanAssertion, it might be useful to also cover assertNull(null) / assertNotNull(null) / assertNull("literallly never null") by a similar rule.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

zbynek and others added 5 commits June 12, 2026 02:10
@zbynek
[java] new rule ConstantInEqualityAssertion for assertEquals
cdc732e
@zbynek
Add more test frameworks, harden condition
9d56485
@zbynek
Add to release notes
af8f242
@zbynek @adangel
Apply suggestions from code review
e83dce0
Co-authored-by: Andreas Dangel <[email protected]>
@zbynek
Reduce test visibility, add comments
5c99ad4
@zbynek
zbynek force-pushed the const-assert branch from 5a53c2c to 5c99ad4 Compare June 12, 2026 00:11
@UncleOwen

UncleOwen commented Jun 12, 2026

Copy link
Copy Markdown
Member

I'm no longer sure this should be a single rule

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@zbynek

zbynek commented Jun 12, 2026

Copy link
Copy Markdown
Contributor Author

What you have right now is AFAICT equivalent to MisorderedAssertEqualsArguments/AssertEqualsArgumentOrderChecker, isn't it?

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 assertEquals(variableX, variableX) -- if yes, and there is a rule EqualsWithItself for it, is it an issue that assertEquals("a","a") would be flagged by two rules?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@UncleOwen

UncleOwen commented Jun 12, 2026

Copy link
Copy Markdown
Member

Ah, I see. Looking at the rule description...

        * Passing a constant as both actual and expected value makes
       the assertion always fail or always pass. In these cases the assertion should be
       replaced by `fail()` or removed.
       * Passing a constant as the actual value and a computed value as the expected value
       indicates that the actual and expected values were swapped, leading to confusing
       error messages should the test fail.

That indeed "feels" like two different things.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@zbynek
zbynek marked this pull request as draft June 12, 2026 21:17
@zbynek
zbynek force-pushed the const-assert branch 2 times, most recently from ba6097a to 2324d7b Compare June 17, 2026 05:51
@zbynek zbynek changed the title [java] New rule: ConstantInEqualityAssertion for assertEquals [java] New rule: AssertEqualsArgumentOrder Jun 17, 2026
@zbynek
Change rule to AssertEqualsArgumentOrder
9c452c5
@zbynek
zbynek force-pushed the const-assert branch from 2324d7b to 9c452c5 Compare June 17, 2026 06:24
@zbynek
zbynek marked this pull request as ready for review June 17, 2026 06:24
@adangel

adangel commented Jun 18, 2026

Copy link
Copy Markdown
Member

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.

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.

Ah, I see. Looking at the rule description...

        * Passing a constant as both actual and expected value makes
       the assertion always fail or always pass. In these cases the assertion should be
       replaced by `fail()` or removed.
       * Passing a constant as the actual value and a computed value as the expected value
       indicates that the actual and expected values were swapped, leading to confusing
       error messages should the test fail.

That indeed "feels" like two different things.

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:

// JUnit 3 and 4, Spring: [message], expected, actual
// JUnit Jupiter: expected, actual, [message]

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

UncleOwen
UncleOwen requested changes Jun 18, 2026
View reviewed changes
Comment thread pmd-java/src/main/resources/category/java/errorprone.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-java/src/main/resources/category/java/errorprone.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread ...st/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/AssertEqualsArgumentOrder.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

@zbynek
Review feedback
a771429
adangel
adangel approved these changes Jun 19, 2026
View reviewed changes

@adangel adangel left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thanks, looks good.

The rule name is good actually, no need to change.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread pmd-java/src/main/resources/category/java/errorprone.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

@zbynek @adangel
Fix warning message
8cc3eaa
Co-authored-by: Andreas Dangel <[email protected]>
UncleOwen
UncleOwen approved these changes Jun 19, 2026
View reviewed changes
@zbynek
zbynek merged commit e931a1c into pmd:main Jun 19, 2026
11 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@UncleOwen UncleOwen added this to the 7.26.0 milestone Jun 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@adangel adangel adangel approved these changes
@UncleOwen UncleOwen UncleOwen approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

a:new-rule Proposal to add a new built-in rule

Projects

None yet

Milestone

7.26.0

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@zbynek @UncleOwen @adangel
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.