Skip to content

[java] JUnitTestsShouldIncludeAssert - false positive with SoftAssertionsExtension #4113

@ctoomey

Description

@ctoomey

Affects PMD Version:

6.48.0+
7.0.0

Rule:

JUnitTestsShouldIncludeAssert

Description:

Rule has false positives when using @ExtendWith(SoftAssertionsExtension.class) for AssertJ soft assertions, which call assertAll automatically so it's not required to call it explicitly in the tests.

Code Sample demonstrating the issue:

import org.assertj.core.api.SoftAssertions;
import org.assertj.core.api.junit.jupiter.InjectSoftAssertions;
import org.assertj.core.api.junit.jupiter.SoftAssertionsExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith({SoftAssertionsExtension.class})
class ShouldIncludeAssertTest {
  @InjectSoftAssertions private SoftAssertions softly;

  @Test
  void falsePositive1() {
    softly.assertThat(false).isTrue();
    softly.assertThat(true).isFalse();
  }

  @Test
  void falsePositive2() {
    SoftAssertions.assertSoftly(
        softly2 -> {
          softly2.assertThat(false).isTrue();
          softly2.assertThat(true).isFalse();
        });
  }
}

Expected outcome:

No violations reported, but PMD incorrectly reports a violation for both falsePositive1 and falsePositive2.

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