Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] JUnitTestsShouldIncludeAssert - false positive with SoftAssertionsExtension #4113

Closed
ctoomey opened this issue Sep 6, 2022 · 1 comment · Fixed by #5334
Closed
Labels
a:false-positive PMD flags a piece of code that is not problematic
Milestone

Comments

@ctoomey
Copy link

ctoomey commented Sep 6, 2022

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

@ctoomey ctoomey added the a:false-positive PMD flags a piece of code that is not problematic label Sep 6, 2022
@adangel adangel changed the title [java] False positive from JUnitTestsShouldIncludeAssert with SoftAssertions [java] JUnitTestsShouldIncludeAssert - false positive with SoftAssertions Sep 8, 2022
@jsotuyod jsotuyod added needs:pmd7-revalidation The issue hasn't yet been retested vs PMD 7 and may be stale and removed needs:pmd7-revalidation The issue hasn't yet been retested vs PMD 7 and may be stale labels Mar 17, 2024
@durimkryeziu
Copy link

I have the same usage of soft assertions as in the falsePositive2() test; I had to @SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert") to make it pass for now 😅

Gradle 8.10
PMD 7.6.0

@adangel adangel changed the title [java] JUnitTestsShouldIncludeAssert - false positive with SoftAssertions [java] JUnitTestsShouldIncludeAssert - false positive with SoftAssertionsExtension Nov 14, 2024
@adangel adangel added this to the 7.8.0 milestone Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:false-positive PMD flags a piece of code that is not problematic
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants