I am currently developing a system that will, hopefully, be used to compare the results of two individual, but identically-named classes. By comparing the two classes, I hope to be able to determine if the outputs from the first classes methods are the same as the outputs from the second classes methods, given the same input parameters.
How would I go about running the same test class on two independent classes, dynamically?
Psudocode example:
If I have a simple test class…
class TestClass{
public int TestMethod(int num){
int num2;
TestedClass t = new TestedClass();
num2 = t.add(num);
}
and two identical classes that need to be tested…
class TestedClass{
public int add(int num){
num = num + 2;
return num;
}
and
class TestedClass{
public int add(int num){
num = num + 4;
return num;
}
How would I go about running the TestClass with respect to the two different TestedClass, and then saving the result of the tests in some form of collection.
As you can see, the two TestedClass’s are identically named and the return types are identical, only the functionality differs slightly. I also do not have access to change the TestedClass’s, however I can control where they are located within the program, package etc.
Thanks in advance.
EDIT:
Appeared to have very cleverly missed out a very important piece of information:
I also do not have access to edit the TestClass, although, as above, I will know the class/method names and have control over where they are located. The idea is to build something that can dynamically test a class given a copy of the input and correct output (the TestClass and the other TestedClass).
Apologies for the confusion.
EDIT 2:
I have access to the tests, ie. I can run them, I just cannot edit them, for example as a few answers suggested.
Edit
You don’t need to run the two tests in the same JVM, you can just call the methods, save the output, and compare using any variety of techniques. Or you could scan for classes the same way old JUnit did it and instantiate via
Class.forName("...").newInstance()and run methods via reflection.