For running all my test classes automatically, I look for all class files inside a dedicated directory, convert the path to a package name and check if this class implements the given interface:
try {
Class<? > myTestClass = Class.forName( constructedClassName );
if( myTestClass.isInstance( MyTestInterface.class ) ) {
testCollection.add( myTestClass );
}
}
catch( Error e ) {
// ignore, no valid test class
}
Today I ran into an ugly bug (see this SO question) using this technique.
Question:
How can I collect all my test classes without having to ignore Errors that can occur with classes I'm not interested in?