Skip to content

[java] ClassNamingConventions: testClassPattern not applied to class that inherits all its @Test methods #4350

@liamsharp

Description

@liamsharp

Affects PMD Version:
6.53.0
7.0.0

Rule:
ClassNamingConventions

Description:

If a class containing no @Test methods inherits from an abstract class that does have @Test methods it's not regarded as a test class itself and bypasses the testClassPattern check as part of ClassNamingConventions

Code Sample demonstrating the issue:

package com.github.screamingfrog;

class Derived extends AbstractUnitTest
{

    // Not detected as a Test class so ClassNamingConventions for testClassPattern not applied
    
    @Override
    int getVal()
    {
        return 0;
    }
    

}
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

abstract public class AbstractUnitTest 
{

    abstract int getVal();
    
    @Test
    public void testApp()
    {
        assertEquals(1, getVal());
    }
}

Expected outcome:

PMD should report this as a violation.

Running PMD through: [ Maven ]

I've created a stand along maven project to reproduce the issues here:
https://github.com/screamingfrog/hello-world-cli/tree/pmd-testclasspattern-issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-negativePMD doesn't flag a problematic piece of code

    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