Skip to content

[java] SimplifiableTestAssertion has a tendency to report false positives #3820

@aaime

Description

@aaime

Affects PMD Version: 6.42

Rule: SimplifiableTestAssertion

Description:

The rule seems to catch usage of assertTrue/assertFalse that contains inside any sort of equal operator, without checking if it's the actual Object equality, nor if there are actual Assert methods that can be used as a replacement.

Code Sample demonstrating the issue:

 // no such thing as a Assert.assertNotEqualsIgnoreCase to replace this (would require full Hamcrest library dependency, not covered by CoreMatchers)
 assertTrue(!"0".equalsIgnoreCase(doc.select("mapml-viewer").attr("zoom")));
 // this equality method takes as extra parameters a comparison tolerance, the signature is
 // public boolean equals(final Envelope envelope, final double eps, final boolean epsIsRelative) {
 assertTrue(outputReader.getOriginalEnvelope().equals(ROI, DELTA, false));
 // no such thing as Assert.assertArraysNotEquals (though easy to replace with Hamcrest CoreMatchers, if available in the classpath, may not be if using JUnit 5)
 Assert.assertFalse(Arrays.equals(destImageRowBand0, srcImageRowBand0));

Expected outcome:

In general, SimplifiableTestAssertion should verify the "equals" call inside the assertion is actually an override of Object.equals, and when suggesting to replace AssertFalse, check that the equivalent inequality assertion is available.

Running PMD through: [Maven]

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematic

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions