Skip to content

[java] JUnitTestsShouldIncludeAssert - false positives with lambdas and static methods #2147

@hgschmie

Description

@hgschmie

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

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions