Unit testing an array of input data
September 26th, 2005
This one is interesting, I think. Suppose I have an array (or Collection) of test input data. I want to test the same Java method for each of those entries. But, when one fails, I want to continue testing, not halt the whole process. How do I go on with this?
- If I create a
testMyMethodX()for every case (with X a number), I do a lot of useless typing. - If, on the other hand, I loop through the array "by hand", the first test that fails will stop all other tests to run.
So, has anybody else encountered this? Did you find a solution? Do you want to share it?

April 21st, 2008 at 10:18 PM Here is potential answer: http://tinyurl.com/dpj4p
April 21st, 2008 at 10:18 PM Generally speaking I take the array approach and just fix each failure until it passes... however, if you wanted to, you could always just replace something like this: Testable[] objs = createObjectsToBeTested(); for (int i=0; i<objs.length; i++) { assertTrue(objs[i].returnTrue()); } with something like this: Testable[] objs = createObjectsToBeTested(); List failures = new ArrayList(); for (int i=0; i<objs.length; i++) { if (!objs[i].returnTrue())) { failures.add(objs[i]); } } for (int i=0; i<failures.size(); i++) { logger.debug("This object failed:" + failures.get(i)); } assertEquals("Failures listed in log output", 0, failures.size());