I'd like to be able to dynamically create TestCases and TestSuites for classes that only differ in the name of the method being tested.
Consider the following, I have a 'calculator' class that contains multiple methods (with different names) that take the same arguments and returns the same results
public class Calc {
public static boolean add(int a, int b, int c){
if ((a+b) == c) then return true;
return false;
}
public static boolean subtract(int a, int b, int c){
if ((a-b) == c) then return true;
return false;
}
public static boolean multiply(int a, int b, int c){
if ((a*b) == c) then return true;
return false;
}
public static boolean divide(int a, int b, int c){
if ((a/b) == c) then return true;
return false;
}
}
I now want to right a JUnit test case for it .... which would look something like this ...
public Class CalcTest extends TestCase{
public void testAdd(){
assert(Calc.add(1,2,3));
}
public void testSubtract(){
assert(Calc.subtract(1,2,3));
}
public void testMultiply(){
assert(Calc.multiply(1,2,3));
}
public void testDivide(){
assert(Calc.divide(1,2,3));
}
}
As you can see, the TestCase itself is made up of methods that are identical except for the name of the function they are trying to test.
My question is: Can you think of anyway of dynamically creating 'methods' that JUnit could call at runtime. Perhaps using reflection?
My aim is to be able to dynamically add/remove tests via a config file. Let's say I only want to run the test for Add, using 'standard' JUnit I would need to code/compile a new TestCase whereas I'd really like to be able to do it dynamically at runtime.
Any ideas?