Skip to content

[java] UnitTestShouldIncludeAssert: False positive with SoftAssertionsExtension on parent/grandparent classes #6431

@kaliy

Description

@kaliy

Affects PMD Version: 7.x (current)

Rule:

UnitTestShouldIncludeAssert

https://docs.pmd-code.org/latest/pmd_rules_java_bestpractices.html#unittestshouldincludeassert

Description:

The UnitTestShouldIncludeAssert rule reports violations for nested test classes that use injected SoftAssertions when the extension is declared on a parent (or grandparent and higher) class

@ExtendWith annotation is inherited by nested clases, so the extension from parent applies there too. Currently PMD recognize the soft assertions only when the extension is applied in the same class.

Code Sample demonstrating the issue:

@ExtendWith(SoftAssertionsExtension.class)
class SomeTest {
    @Nested
    class NestedTest {
        @Test
        void nestedTest(SoftAssertions softly) { 
            softly.assertThat(42).isEqualTo(42);
        }
    }
}

Expected outcome:

PMD reports a violation at line 6, but that's wrong. That's a false positive.

Running PMD through: Gradle

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