Skip to content

[java] New rule: ControlFlowInTest #7116

Closed

Description

@zbynek

Proposed Rule Name: ControlFlowInTest

Proposed Category: Best Practices

Description:
Using control flow statements such as if, switch or throw-catch as well as ternary conditional operator in tests makes it hard to reason about the correctness of tests. It increases the chance that the test contains the same logical errors as the code under test and it can obscure the fact that some assertions are not executed. Arguably the same is true about loops.

The ifs and ternary operators can be avoided by using concrete examples. The try-catch statements should be replaced by assertThrows or assetDoesNotThrow.

Code Sample:

// bad: not obvious that years divisible by 400 are not covered
@Test
testLeapYearBad() {
  for (int y = 0; y < 3000; y++) {
    if (y % 100  == 0 && y % 400 != 0) { // violation
      assertFalse(leapYear(y));
    } else if (y % 4 == 0) { // violation
      assertTrue(leapYear(y));
    }
  }
}

// good: using concrete numbers
@Test
testLeapYearGood() {
  assertTrue(leapYear(2000));
  assertFalse(leapYear(1000));
  assertTrue(leapYear(2004));
  assertFalse(leapYear(2005));
}

Possible Properties:

  • Turn on/off for specific kinds of control flow: allowIf, allowTernary, allowSwitch, allowLoops, allowThrow -- most should be false by default, except for allowLoops.

Activity

  1. added
    a:new-rule
    on Sep 26, 2026
  2. MichalKollarcik commented on Oct 8, 2026

    @MichalKollarcik
    Contributor

    Hi! I'm interested in implementing this rule, including tests and documentation. I'm currently getting familiar with the codebase. Is this proposal still available to work on, and would you be open to a PR implementing it? Are there any scope decisions we should agree on first?

  3. zbynek commented on Oct 8, 2026

    @zbynek
    ContributorAuthor

    Yes, AFAIK nobody else is working on this. I think this would be better implemented as a Java rule, since XPath rule could get too complex.

    Maybe @UncleOwen or @adangel could confirm that the proposed scope outlined in issue description makes sense.

  4. UncleOwen commented on Oct 8, 2026

    @UncleOwen
    Member

    What's the use case for allowing some, but no all flow control statements?

  5. zbynek commented on Oct 8, 2026

    @zbynek
    ContributorAuthor

    What's the use case for allowing some, but no all flow control statements?

    I tried to apply this rule to PMD's own tests and while I was able to reduce ifs and trys quite a bit, resulting in IMHO more readable code in most cases, but I don't think removing loops would improve things (it's possible to replace them with streams and lambdas.

    #7173

    But maybe the proposed properties are too granular and allowLoops is the only one needed for the first iteration and more can be added if someone finds a use-case?

  6. UncleOwen commented on Oct 9, 2026

    @UncleOwen
    Member

    Yes, only allowLoops sounds like a good idea to start with.

    (I'd even argue that loops should be replaces with parameterized tests... but maybe that's not possible in all cases)

  7. changed the title [-][java] new rule: ControlFlowInTest[/-] [+][java] New rule: ControlFlowInTest[/+] on Oct 27, 2026
  8. added this to the 7.30.0 milestone on Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:new-rule

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions