unit-testingblack-boxwhite-boxwhite-box-testingblack-box-testing

should unit tests be black box tests or white box tests?


Say I have three methods, all very similar but with different input types:

void printLargestNumber(int a, int b) { ... }
void printLargestNumber(double a, double b) { ... }
void printLargestNumber(String numberAsString, String numberAsString) { ... }

All three use the same underlying logic. For example: maybe the double version is the only one that compares numbers, and the other two just convert their inputs to double.

We could imagine a few different unit tests: first input is larger, second is larger, both inputs are negative, etc.

My Question

Should all three methods have the full set of tests (black box since we don't assume the core implementation is the same)

or

Should only the double version be tested heavily and the other two tested lightly to verify parameter conversion (white box testing since we know they share the same implementation and it's already been tested in the double tests)?


Solution

  • If all of those methods are public, i.e. callable by the outside world, I'd definitely test all of them with a full set of tests. One good reason is that white-box tests are more brittle than black-box tests; if the implementation changes the public contract might change for some of those methods.