Skip to content

[java] Don't raise JUnitTestContainsTooManyAsserts on JUnit 5's assertAll #1212

@krichter722

Description

@krichter722

Affects PMD Version: 6.5.0-SNAPSHOT

Rule: JUnitTestContainsTooManyAsserts

Description:

The Analysis seems to detect more than one assertTrues inside a JUnit 5 assertAll which represents a logical combination of assertions and thus one assertion. Such a setup should thus be treated as one assertion and JUnitTestContainsTooManyAsserts shouldn't be raised if no other assert[...] is present in the method.

Code Sample demonstrating the issue:

public class SomeClass {

    public int someMethod(MutableInteger mutableInteger) {
        mutableInteger.setValue(mutableInteger.getValue()+1);
        return -1;
    }
}

public class SomeClassTest {

    @Test
    public void testSomeMethod() {
        MutableInteger mutableInteger = new MutableInteger(2);
        SomeClass instance = new SomeClass();
        int expResult = -1;
        int result = instance.someMethod(mutableInteger);
        assertAll(() -> assertEquals(expResult, result),
            () -> assertEquals(3, mutableInteger.getValue()));
    }
}

public class MutableInteger {
    private int value;

    public MutableInteger(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }
}

See https://gitlab.com/krichter/pmd-asssert-all/ for project download and https://gitlab.com/krichter/pmd-asssert-all/-/jobs/77540065 for example CI output.

Running PMD through: Maven

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions