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] TooManyMethods triggers for JUnit tests #1611

Open
mcandre opened this issue Jan 21, 2019 · 2 comments
Open

[java] TooManyMethods triggers for JUnit tests #1611

mcandre opened this issue Jan 21, 2019 · 2 comments

Comments

@mcandre
Copy link

mcandre commented Jan 21, 2019

Affects PMD Version:

3.9+

Rule:

TooManyMethods

Description:

This rule can trigger when a JUnit test is thorough, featuring multiple tests. As a workaround, I am disabling this rule in my rulesets.xml:

<rule ref="category/java/design.xml">
    <exclude name="TooManyMethods" />
</rule>

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

Gradle

@linusjf
Copy link

linusjf commented May 26, 2020

//ClassOrInterfaceDeclaration/ClassOrInterfaceBody
     [
      count(./ClassOrInterfaceBodyDeclaration/MethodDeclaration[
         not (
                starts-with(@Name,'get')
                or
                starts-with(@Name,'set')
                or
                starts-with(@Name,'is')
            )
      ]) > $maxmethods
   ]

The rule currently excludes getters, setters and is methods.
Methods such as 'are', 'test' and having annotations like @test signifying a unit test could be added so that test classes don't trigger this rule.

@linusjf
Copy link

linusjf commented Nov 3, 2020

//ClassOrInterfaceDeclaration/ClassOrInterfaceBody
     [
      count(./ClassOrInterfaceBodyDeclaration/MethodDeclaration[
         not (
                starts-with(@Name,'get')
                or
                starts-with(@Name,'set')
                or
                starts-with(@Name,'is')
            )
      ]) > $maxmethods
   ]

The rule currently excludes getters, setters and is methods.
Methods such as 'are', 'test' and having annotations like @test signifying a unit test could be added so that test classes don't trigger this rule.

Another option is to use Nested classes like JUnit5 provides.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants