I’m trying to write some code which recursively adds TestSuites in a project to a suite of suites located at the root of the package hierarcy.
I’ve already written the code which returns a Collection object which contains a File object for each Test Suite found in my project.
I’m now trying to loop through them and add them to a TestSuite in a file called AllTests.java:
public static Test suite() throws IOException, ClassNotFoundException {
TestSuite suite = new TestSuite();
//Code not included for getTestSuites() in this snippet.
Collection<File> testSuites = getTestSuites();
for(File f: testSuites) {
//Truncate the path of the test to the beginning of the package name
String testName = f.getAbsolutePath().substring(f.getAbsolutePath().lastIndexOf("net"));
//Replace backslashes with fullstops
testName = testName.replaceAll("\\\\", ".");
//Take the .class reference off the end of the path to the class
testName = testName.replaceAll(".class", "");
//Add TestSuite to Suite of Suites
Class<? extends Test> test = (Class<? extends Test>) AllTests.class.getClassLoader().loadClass(testName);
suite.addTest(test);
}
Unfortunately I am getting the following compiler error on the suite.addTest(test) line:
The method addTest(Test) in the type
TestSuite is not applicable for the
arguments (Class < capture#3-of ? extends Test>)
Am I making a fundamental mistake by assuming that a Class< Test > reference and a Test reference are one and the same?
Yes, you are making a fundamental mistake by assuming that a Class< Test > reference and a Test reference are one and the same.
You need an instance of a Class that extends
Test, not an instance of a Class object whose definition extendsTest(Classes are objects too in java).