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 positives with lambdas and static methods #2147

Closed
hgschmie opened this issue Dec 6, 2019 · 1 comment · Fixed by #2899
Closed
Labels
a:false-positive PMD flags a piece of code that is not problematic
Milestone

Comments

@hgschmie
Copy link
Contributor

hgschmie commented Dec 6, 2019

Affects PMD Version:

6.20.0

Rule:

JUnitTestsShouldIncludeAssert

Description:

asserts in static method calls and in lambdas are ignored, triggering false positives

Code Sample demonstrating the issue:

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import com.fasterxml.jackson.databind.JsonNode;


@Test
public void testDemo() {
  JsonNode jsonNode = assertSuccessResponse();
  final int totalFieldsCount = jsonNode.get("count").asInt();
  jsonNode.get("data").forEach(item -> assertTrue(item.size() <= totalFieldsCount));
}

 public static JsonNode assertSuccessResponse() {
    JsonNode jsonNode = doStuffToBuildTheJsonNode();
    assertTrue(jsonNode.hasNonNull("count"));
    assertTrue(jsonNode.hasNonNull("data"));
    assertTrue(jsonNode.get("count").asInt() > 0);
    assertEquals(jsonNode.get("count").asInt(), jsonNode.get("data").size());
    return jsonNode;
}

(In the actual failing code, the assertSuccessResponse method is in a different class as a static helper method).

[INFO] PMD Failure: TheTest:98 Rule:JUnitTestsShouldIncludeAssert Priority:3 JUnit tests should include assert() or fail().

Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]

Maven, OpenJDK from AdoptOpenJDK 1.8

@adangel adangel added the a:false-positive PMD flags a piece of code that is not problematic label Oct 3, 2020
@adangel adangel changed the title [java] JUnitTestsShouldIncludeAssert - false positives [java] JUnitTestsShouldIncludeAssert - false positives with lambdas and static methods Oct 3, 2020
oowekyala added a commit to oowekyala/pmd that referenced this issue Nov 5, 2020
@oowekyala oowekyala added this to the 7.0.0 milestone Nov 10, 2020
@oowekyala oowekyala linked a pull request Nov 10, 2020 that will close this issue
5 tasks
@adangel
Copy link
Member

adangel commented Dec 11, 2020

Fixed via #2899 for PMD 7.

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.

3 participants