Skip to content

[java] New rule: AssertStatementInTest#6557

Merged
adangel merged 10 commits into
pmd:mainfrom
zbynek:assert-test
May 15, 2026
Merged

[java] New rule: AssertStatementInTest#6557
adangel merged 10 commits into
pmd:mainfrom
zbynek:assert-test

Conversation

@zbynek
Copy link
Copy Markdown
Contributor

@zbynek zbynek commented Mar 28, 2026

Describe the PR

The new rule suggests using framework assertions instead of assert statement in tests.
The proposed implementation enforces that both in test methods and test setup/teardown methods, since in both cases the error messages are processed by the test runner.

Related issues

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)

@pmd-actions-helper
Copy link
Copy Markdown
Contributor

pmd-actions-helper Bot commented Mar 28, 2026

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 1 new violations, 0 new errors and 0 new configuration errors,
removes 212 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-05-13 23:56:52+00:00 for 93533b8)

@adangel adangel changed the title [java] new rule AssertStatementInTest [java] New rule: AssertStatementInTest May 7, 2026
@adangel adangel added the a:new-rule Proposal to add a new built-in rule label May 7, 2026
@adangel adangel added this to the 7.25.0 milestone May 7, 2026
Copy link
Copy Markdown
Member

@adangel adangel left a comment

Choose a reason for hiding this comment

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

Thanks!

Can you additionally add a test case for Junit5 - as we mention it in the rule description, we should verify it.

Also, could you add a section in the release_notes describing the new rule?
Something like

### 🌟️ New Rules
* The new Apex rule {% rule apex/errorprone/AvoidInterfaceAsMapKey %} reports `Map` declarations
(fields, variables, parameters) whose key type is an interface that has at least one abstract implementing
class defining `equals` or `hashCode`. Using such maps results in potentially duplicated map entries or
not being able to get entries by key.
* The new Java rule {% rule java/multithreading/OverridingThreadRun %} finds overridden `Thread::run` methods.
This is not recommended. Instead, implement `Runnable` and pass an instance to the thread constructor.

Comment thread pmd-java/src/main/resources/category/java/bestpractices.xml Outdated
@zbynek zbynek requested a review from adangel May 13, 2026 23:50
Copy link
Copy Markdown
Member

@adangel adangel left a comment

Choose a reason for hiding this comment

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

Thanks!

* {% rule java/codestyle/UnnecessaryModifier %}
* {% rule java/design/UseUtilityClass %}

### 🌟️ New and Changed Rules
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for providing the text for the release notes.

I'll move this up when merging - this section should be before "Changed rules"...

@adangel
Copy link
Copy Markdown
Member

adangel commented May 15, 2026

Oh - at some point, we should document, that SignatureDeclareThrowsException ignores test methods... we have an option for junit3. But for me it is not clear in the documentation, that it always ignores any test framework related methods.

adangel added a commit that referenced this pull request May 15, 2026
@adangel adangel merged commit 1e9c69c into pmd:main May 15, 2026
13 checks passed
adangel added a commit that referenced this pull request May 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[java] New rule: AssertStatementInTest

2 participants