Imagine the following situation: There is a legacy application I want to write tests for in order to be able to refactor the (ugly) code.
Now there is a big chunk of code that deals with some constraints. Basically it boils down to this signature:
public Boolean isValidRequest(Boolean constraint0, Boolean constraint1, Boolean constraint2, BooleanConstraint3) {
return constraint0 && constraint1 && constraint2 && constraint3;
}
Now what I want to test is exactly that "chaining" (here all constraints are chained with "&&", but I want to make sure this stays like it, e.g. no typo!)
In order to test all combinations I would have to write n² tests where n is the number of constraints. Here it would be 4², hence 16 tests. I wonder if there is a more easy way. The only valid testcase is:
@Test
public void testAllConstraintsFulfilled() {
assertThat(myObjectToTest.isValidRequest(true, true, true, true)).isTrue();
}
While all other combinations should fail. I wonder whether there is a way I don't know about to skip manually writing the other 15 testcases and instead say something like:
1) Try all combinations
2) Only (true, true, true, true) should not fail
I have mockito, assertj and junit4 available. Any hints, ideas?
There are two ways here:
A) you turn to coverage; and you keep writing test cases until you hit 100%. You don't need to test all possible paths - only the existing paths that your program can take!
B) you reverse that approach: instead of writing testcases to hit paths; you use a solution that generates tests for you. In other words: you provide rules, and that an engine verifies that the code under test matches those rules. QuickCheck would be one example of such tooling.