Assume you have a method check() of a class in your Business Layer that does some validations on instances of this class. The checks are of the form if attribute a of that class has a certain value, attribute b needs to have a certain other value (that is a determines b). The method could for instance internally use a map containing all valid combinations of a and b:
public bool check() {
if (mapOfValidCombos.get(a)!=b)
return false;
return true;
}
or it could look them up in an external csv file or it could consist of a list of if statements:
public bool check() {
if (a==1) and (b!=5) then return false;
if (a==2) and (b!=3) then return false;
return true;
}
Now if you would like to unit test that method. What are the testcases you should implement:
- You could test for all valid combinations of a and b, and also the invalid ones (assuming a and b are enumerable types)
- You could only create one testcase per path of that method (e.g. one for a valid combination, one for an invalid one)
Approach number 2 result in less unit testcases (and hence less maintenance effort for them). However where is the right place to actually test that the method is doing the right checks from a business perspective (e.g. if the map of valid combinations has the correct values in it?).
If you go for approach number 1 should you add additional testcases if you change the implementation from lets say Map bases to if statements since you get more executtion paths of your method?
EDIT: I was thinking more in terms of what value these tests would add to our test suite. Let's say if go for a map based implementation of the check() method what use is there to duplicate the content of the map into my test case. Regarding hidro hint to use paraterized tests. They still need to get their parameters from somewhere (and most likely not out of check()'s implementation) so the values are somehow duplicated. I was thinking more along the line that the test of the actual values and the completeness of the check method is something done by QA or a business departement in acceptance testing, where as my unit test just check the execution paths of my code and not necessarily all the combinations of allowed values. Any thoughts?