javaunit-testingjunitcode-coveragemc-dc

Test coverage for many AND/OR conditions in one statement


Example statement:

if (conditionA && conditionB && conditionC && conditionD) {
    return true;
}

I could write unit tests for all 2^4 combinations, but that would easily go out of hand if more conditions are added.

What should be my unit testing strategy to cover all conditions for a statement like this? Is there any other way to make the code more robust?


Solution

  • The way I see this scenario is 1 happy path, and 4 potential points of failure. If each condition is pivotal to allowing true to be returned, then it would be reasonable to write:

    1. A single happy-path unit test, the only case where the logic returns true. And
    2. A unit test for each variable that could cause the check to fail, asserting that the single variable has the power to prevent the condition from passing.

    I understand it's possible to write logic that passes these checks, but actually returns true when multiple variables are false... but I really wouldn't worry about cases like that unless you're working on a spaceship or something where life / death is involved. In almost all cases, the tester is just testing that the implementation fails on the failure of any variable.